24

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?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Arief
  • 6,055
  • 7
  • 37
  • 41
  • JavaScript could be bad, because a lot of users have their time set inaccurately. Also, it could be abused by someone. You have to validate whether the offset to UTC is reasonable or not for sure. – hangy Nov 08 '08 at 15:32

6 Answers6

17

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.

Ryan
  • 7,835
  • 2
  • 29
  • 36
  • this line is not clear Int32.TryParse(cookie.Value, out result); you store the cookie value in result variable but before u store total minute to result variable. not very clear your server side routine GetTimeZoneOffset() function. if possible plzz explain. – Thomas Aug 19 '14 at 11:20
  • @Thomas I started with the Server time zone and then check to see if the client time zone cookie exists. If the cookie exists, try convert the string to the integer minutes. – Ryan Aug 20 '14 at 12:17
12

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.

Simon Johnson
  • 7,802
  • 5
  • 37
  • 49
5

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

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
0

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.

Keltex
  • 26,220
  • 11
  • 79
  • 111
  • 3
    IP won't necessarily give the correct answer. I have a client who use a single site for all network connectivity (direct pipes from one site to another) so all of them are coming from a CST IP regardless of where they're actually at. – Stephen Wrighton Nov 08 '08 at 16:26
  • Stephen: We do the business to hide client IPs so VPN-in is in Prague and proxy-out is in Berlin. And clients are, for example, in Moscow. – abatishchev Feb 26 '11 at 01:46
0

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.

JacquesB
  • 41,662
  • 13
  • 71
  • 86
-5

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.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 6
    Welcome to StackOverflow! Please re-read the question. The user wants to know the local time of the client, not the time format. – Bob Kaufman Jun 27 '12 at 20:00