How to Get Client Date and Time in ASP.NET

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



Leave a reply



Submit