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?
No,
DateTime.Nowwill 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.UtcNowand 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.)