Click here to Skip to main content
15,112,354 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 7:02
professionalSander Rossel10-May-21 7:02 
GeneralRe: Just have to admire... Pin
Nelek10-May-21 7:36
protectorNelek10-May-21 7:36 
GeneralThought of the Day Pin
OriginalGriff10-May-21 5:45
mveOriginalGriff10-May-21 5:45 
GeneralRe: Thought of the Day Pin
megaadam10-May-21 5:55
professionalmegaadam10-May-21 5:55 
GeneralRe: Thought of the Day Pin
raddevus10-May-21 6:30
mvaraddevus10-May-21 6:30 
GeneralRe: Thought of the Day Pin
W Balboos, GHB10-May-21 7:05
mveW Balboos, GHB10-May-21 7:05 
GeneralRe: Thought of the Day Pin
DRHuff10-May-21 10:25
MemberDRHuff10-May-21 10:25 
GeneralHow did I not know this? Pin
Sander Rossel10-May-21 4:30
professionalSander Rossel10-May-21 4: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? Pin
SeanChupas10-May-21 4:42
MemberSeanChupas10-May-21 4:42 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 5:35
professionalSander Rossel10-May-21 5:35 
GeneralRe: How did I not know this? Pin
Mircea Neacsu10-May-21 6:02
mvaMircea Neacsu10-May-21 6:02 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 6:36
professionalSander Rossel10-May-21 6:36 
GeneralRe: How did I not know this? Pin
Mircea Neacsu10-May-21 7:15
mvaMircea Neacsu10-May-21 7:15 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 23:09
professionalSander Rossel10-May-21 23:09 
GeneralRe: How did I not know this? Pin
SeanChupas10-May-21 8:05
MemberSeanChupas10-May-21 8:05 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 23:10
professionalSander Rossel10-May-21 23:10 
GeneralRe: How did I not know this? Pin
Jörgen Andersson10-May-21 8:53
professionalJörgen Andersson10-May-21 8:53 
GeneralRe: How did I not know this? Pin
Sander Rossel10-May-21 23:15
professionalSander Rossel10-May-21 23:15 
GeneralRe: How did I not know this? Pin
Jörgen Andersson11-May-21 0:23
professionalJörgen Andersson11-May-21 0:23 
GeneralRe: How did I not know this? Pin
Sander Rossel11-May-21 0:43
professionalSander Rossel11-May-21 0:43 
GeneralRe: How did I not know this? Pin
Jörgen Andersson11-May-21 0:46
professionalJörgen Andersson11-May-21 0:46 
GeneralRe: How did I not know this? Pin
Sander Rossel11-May-21 1:28
professionalSander Rossel11-May-21 1:28 
GeneralRe: How did I not know this? Pin
StarNamer_11-May-21 4:39
professionalStarNamer_11-May-21 4:39 
GeneralRe: How did I not know this? Pin
Sander Rossel11-May-21 12:04
professionalSander Rossel11-May-21 12:04 
GeneralRe: How did I not know this? Pin
Fabio Franco12-May-21 1:35
professionalFabio Franco12-May-21 1: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.