I'm trying to work out how to retrieve the current system timezone in a (TZ) format on windows, ie. America/New_York, I need to supply this to an API this application communicates with.
I'm currently using
TimeZone.CurrentTimeZone
Which gives me this output
GMT Standard Time
What I hope to get is something like
Europe/London
Am I missing something simple or is this not available and thus does that mean I need to do the conversion myself?
I recommend to use NodaTime for that.
You can get the timezone of your system like that :
DateTimeZone tz = DateTimeZoneProviders.Tzdb.GetSystemDefault();
It will get the IANA Timezone as you need if you use tz.ToString()
(and apart from that, it is a very nice open source library that handles timezone, datetimes, instants and calendars in a IMHO much more structured and reliable way than the builtin .NET DateTime classes).
NodaTime is maintained and well supported by some high rep users here in SO ;) .
For information, the output that you are getting and you don't want, the one used by .NET, is called BCL Timezone, but you want the IANA Timezone (or TZDB) (which is more accurate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With