How to get client date and time in ASP.NET?

When I use DateTime.Now I get the date and time from the server point of view. Is there any way to get the client date and time in ASP.NET?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I like the idea of either using the browser/system time and time zone or letting them select their time zone. In a past project I used something like this:

<script language="javascript">
function checkClientTimeZone()
{
    // Set the client time zone
    var dt = new Date();
    SetCookieCrumb("ClientDateTime", dt.toString());

    var tz = -dt.getTimezoneOffset();
    SetCookieCrumb("ClientTimeZone", tz.toString());

    // Expire in one year
    dt.setYear(dt.getYear() + 1);
    SetCookieCrumb("expires", dt.toUTCString());
}

// Attach to the document onload event
checkClientTimeZone();
</script>

And then on the server:
/// <summary>
/// Returns the client (if available in cookie) or server timezone.
/// </summary>
public static int GetTimeZoneOffset(HttpRequest Request)
{
    // Default to the server time zone
    TimeZone tz = TimeZone.CurrentTimeZone;
    TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
    int result = (int) ts.TotalMinutes;
    // Then check for client time zone (minutes) in a cookie
    HttpCookie cookie = Request.Cookies["ClientTimeZone"];
    if (cookie != null)
    {
        int clientTimeZone;
        if (Int32.TryParse(cookie.Value, out clientTimeZone))
            result = clientTimeZone;
    }
    return result;
}

Or you can pass it in as a URL parameter and handle it in the Page_Load:
http://host/page.aspx?tz=-360

Just remember to use minutes, since not all time zones are whole hours.

Method 2

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.

Method 3

if you’re maintaining a user profile, you can ask them to tell you their timezone, and then do the calculations necessary.

Method 4

The alternative is to geolocate the user based on his/her IP address. Or geolocate if the browser has this capability (coming soon for Firefox). Once you have the user’s location you can lookup the timezone.

The javascript solution is probably a good and easy one.

Method 5

Client and server may not be totally synchronized, so the question is if you want the time on the client computer, or you want the time on the server, but adjusted for time-zone differences. Javascript can get you the time on the client (including timezone). You can also combine the time on the server with the timezone of the client.

You can never get the time with higher precision than the latcency of a request, though.

Method 6

I used this method in ASP.Net with VB

Dim strLanguage As String = Request.UserLanguages(0)
Dim currentCulture As CultureInfo = CultureInfo.CreateSpecificCulture(strLanguage)
Dim dateformat As String = currentCulture.DateTimeFormat.ShortDatePattern

This will yield the data time format of the computer looking at the data.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x