Home > Mobile >  Creating a DateTimeOffset in a Timezone Different to the One the User is In
Creating a DateTimeOffset in a Timezone Different to the One the User is In

Time:12-19

I want to create a DateTimeOffset which is the date that I specify, but in another timezone. There's a bit of a catch to this. 2 things:
-1- I am NOT in the TimeZone in which I am interested i.e. it is NOT my local timezone.
-2- At the time of writing, the offset in Auckland is non-standard due to Daylight savings. Normally it is 12. right now, it is 13.

So, my attempt was as follows:

var someDate = new DateTimeOffset(selectedDate, TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time").BaseUtcOffset);

This did not work because BaseUtcOffset always returns the standard offset i.e. 12, not 13.

How do I got about this.
Remember, before suggesting conversions to UTC etc. that I am not in that TimeZone. So any date I create on my computer will be in the E. Australia Daylight Time timezone, not New Zealand Standard Time.

CodePudding user response:

You can use this to get the correct offset on the date of whatever selectedDate is:

TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time").GetUtcOffset(selectedDate)

If you feed that today's date, it returns 13.

It's probably best if selectedDate is UTC. I don't know what effect it would have if it's a local time.

  • Related