Click here to Skip to main content
15,880,891 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Just have to admire... Pin
Sander Rossel10-May-21 6:02
professionalSander Rossel10-May-21 6:02 
GeneralRe: Just have to admire... Pin
Nelek10-May-21 6:36
protectorNelek10-May-21 6:36 
GeneralThought of the Day Pin
OriginalGriff10-May-21 4:45
mveOriginalGriff10-May-21 4:45 
GeneralRe: Thought of the Day Pin
megaadam10-May-21 4:55
professionalmegaadam10-May-21 4:55 
GeneralRe: Thought of the Day Pin
raddevus10-May-21 5:30
mvaraddevus10-May-21 5:30 
GeneralRe: Thought of the Day Pin
W Balboos, GHB10-May-21 6:05
W Balboos, GHB10-May-21 6:05 
GeneralRe: Thought of the Day Pin
DRHuff10-May-21 9:25
DRHuff10-May-21 9:25 
GeneralHow did I not know this? Pin
Sander Rossel10-May-21 3:30
professionalSander Rossel10-May-21 3:30 
So I'm working on an Azure app and one thing I do (and have done for 11 years now) is use DateTime.Now, which gives me the current system date and time.
Never been an issue with WinForms apps or locally hosted apps.
It's even never been an issue in web apps where I've used it for logging and the like.
I've had to use specific time zones before, but always the user's client time zone.

In this particular case, I need to save the system/company date and time and also report this back to the user.
Azure uses UTC and I'm in The Netherlands though, so the time is always off by one or two hours (depending on summer or winter).
The fix, apparently, is this:
C#
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
var now = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timeZone);
The "W. Europe Standard Time" is a bit of a magical string that will probably change in a few years when we're abolishing summer time (so make it a setting and don't hard code it!).
You can get a list of these magical strings using tzutil /l (on Windows).
I've always known DateTime.Now isn't really safe or anything, it's just never been an issue until now.
DateTime.Today is specific and accurate enough for almost everything I do.

And so I've learned how to get the date and time for a specific timezone after 11 years of programming Smile | :)

GeneralRe: How did I not know this? PinPopular
SeanChupas10-May-21 3:42
SeanChupas10-May-21 3:42 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 4:35
professionalSander Rossel10-May-21 4:35 
GeneralRe: How did I not know this? Pin
Mircea Neacsu10-May-21 5:02
Mircea Neacsu10-May-21 5:02 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 5:36
professionalSander Rossel10-May-21 5:36 
GeneralRe: How did I not know this? Pin
Mircea Neacsu10-May-21 6:15
Mircea Neacsu10-May-21 6:15 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 22:09
professionalSander Rossel10-May-21 22:09 
GeneralRe: How did I not know this? Pin
SeanChupas10-May-21 7:05
SeanChupas10-May-21 7:05 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 22:10
professionalSander Rossel10-May-21 22:10 
GeneralRe: How did I not know this? Pin
Jörgen Andersson10-May-21 7:53
professionalJörgen Andersson10-May-21 7:53 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 22:15
professionalSander Rossel10-May-21 22:15 
GeneralRe: How did I not know this? Pin
Jörgen Andersson10-May-21 23:23
professionalJörgen Andersson10-May-21 23:23 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 23:43
professionalSander Rossel10-May-21 23:43 
GeneralRe: How did I not know this? Pin
Jörgen Andersson10-May-21 23:46
professionalJörgen Andersson10-May-21 23:46 
GeneralRe: How did I not know this? Pin
Sander Rossel11-May-21 0:28
professionalSander Rossel11-May-21 0:28 
GeneralRe: How did I not know this? Pin
StarNamer@work11-May-21 3:39
professionalStarNamer@work11-May-21 3:39 
GeneralRe: How did I not know this? Pin
Sander Rossel11-May-21 11:04
professionalSander Rossel11-May-21 11:04 
GeneralRe: How did I not know this? Pin
Fabio Franco12-May-21 0:35
professionalFabio Franco12-May-21 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.