views:

57

answers:

3

I have a website that displays the current date using the code Datetime.Now. From what I understand, Datetime.Now is supposed to display the current time as it would appear in the viewer's current locale.

At the moment, when I test on localhost, the website is correctly determining my location (en-nz) and displaying the right date. However, when I run the site live, I'm getting a different date all together.

So how does a system determine a viewer's locale and why is there a difference between how my site is displaying Datetime.Now locally and live?

+5  A: 

No, DateTime.Now will retrieve the current time in the local time zone of the machine it's running on. In other words, the web server in your case, assuming you're writing a web app (you haven't made it clear).

If you want to display it in the local time zone of a browser, you may well be best to send down DateTime.UtcNow and write some JavaScript to convert that to the local time... or just let JavaScript work out the current time on the user's system.

As far as I know, there's no way of getting the time zone from JavaScript accurately. You can get the current offset from UTC, but that's not the same as the time zone itself. (Offsets change due to things like DST; knowing the current offset doesn't tell you when DST will kick in.)

Jon Skeet
+6  A: 

DateTime.Now has the time of the server, but not the time of the client. If you want to detect the time of the client you need to use Javascript, either by detecting the time with it or calculating it using the time zones.

In your localhost works fine, since the server and the client are in the same PC/Time Zone

Hope it helps!

David Conde
A: 

Maybe machines have different time settings? If development and live servers are in different locations, that's quite possible. As other answers say, DateTime.Now gets server time, not client time.

antur123