How to get client current date and time in asp.net?
Below is the javascript function to get client current datetime:
<script type="text/javascript">
function GetDate(date) {
CurTime = new Date(date);
var offset = (new Date().getTimezoneOffset() / 60) * (-1);
var utc = CurTime.getTime() + (offset * 60000 * (-1));
var serverDate = new Date(utc + (3600000 * offset));
var dateString = (serverDate.getMonth() + 1) + "/" + serverDate.getDate() + "/" +
serverDate.getFullYear() + " " + serverDate.toLocaleTimeString("en-US", { hour12: true });
}
</script>
You can also get OffSet Time from code behind as below:
public static TimeSpan GetOffSetTime(string date)
{
DateTime d = Convert.ToDateTime(date);
TimeZone zone = TimeZone.CurrentTimeZone;
TimeSpan local = zone.GetUtcOffset(d);
return local;
}
How to get client date and time in ASP.NET?
What I'd do is create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.
The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.
The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.
Alternatively, you could do this with AJAX but the implementation will depend on which library you use.
How to get the date time of client PC in asp.net C#?
modified code from This link:
<script type="text/javascript">
function getDateTime()
{
var localTime = new Date();
var year= localTime.getYear();
var month= localTime.getMonth() +1;
var date = localTime.getDate();
var hours = localTime .getHours();
var minutes = localTime .getMinutes();
var seconds = localTime .getSeconds();
//at this point you can do with your results whatever you please
}
</script>
At this point i would concatenate all of the fields together and put them in an asp:HiddenField control so they can be read on the server. On the server, call Convert.ToDateTime() on the Text value of your HiddenField.
How to get current DateTime from client?
First Solution:
using Blazored.Localisation nuget package
in Package Manager Console
Install-Package Blazored.Localisation
in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddBlazoredLocalisation(); // This adds the IBrowserDateTimeProvider to the DI container
}
and in your view
@inject Blazored.Localisation.Services.IBrowserDateTimeProvider browserDateTimeProvider
@page "/"
<p>The current local time is: <strong>@currentLocalTime</strong></p>
@code {
string currentLocalTime = "";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender) // Remove the firstRender check if you want the current local time displayed to continuously update.
{ // Leave the above firstRender check in place to ensure that the call to StateHasChanged() does not trigger an endless update loop.
var browserDateTime = await browserDateTimeProvider.GetInstance();
currentLocalTime = browserDateTime.Now.ToString();
StateHasChanged();
}
}
}
Second Solution:
like @Ibrahem Uwk said, add function in Separate JavaScript file and include it in _Host.cshtml or any file but not .razor file
Code in JavaScript file
function getClientDate() {
var d = new Date();
document.getElementById("demo").innerHTML = d;
}
and then call the function in your View
@page "/"
@inject IJSRuntime jsRuntime
...
<p id="demo"></p>
...
@code {
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// call your JS here
JSRuntime.InvokeVoidAsync("getClientDate");
}
}
}
Related Topics
How to Combine More Than Two Generic Lists in C# Zip
Convert a Binary String Representation to a Byte Array
Viewmodels in MVC/Mvvm/Separation of Layers- Best Practices
Finding an Enum Value by Its Description Attribute
Linq Select Distinct with Anonymous Types
Why Must a Lambda Expression Be Cast When Supplied as a Plain Delegate Parameter
Entity Framework Self Referencing Loop Detected
How to Build a Datatemplate in C# Code
How to Calculate Wpf Textblock Width for Its Known Font Size and Characters
Transparency for Windows Forms Textbox
Get the Value for a Listbox Item by Index
How to Serialize/Deserialize a Custom Collection with Additional Properties Using JSON.Net
System.Text.JSON API Is There Something Like Icontractresolver
Difference Between Invoke and Dynamicinvoke