❔ Specify timezone of DateTime converted from localized timestamp without changing the time?
I'm working with an API that returns timestamps localized to Eastern Standard Time. These are encoded as Unix timestamps (seconds since 1970-01-01 00:00:00). I need to convert the timestamp to a DateTime object and then specify that the DateTime is reflecting a specific time zone. I do not want to convert the value as I don't want to add or subtract hours; I just want to specify that the value is already GMT -6. I need this because I need to compare the time against real Eastern Standard Time, which is at different times of the year either Eastern Standard Time or Eastern Daylight Time.
So how can I specify the time zone of a DateTime sourced from a localized timestamp?
6 Replies
DateTime doesn't have the concept of a time zone. You might want to consider using DateTimeOffset (which tells you the time relative to UTC), although that doesn't necessarily account for things like Daylight Saving Time.
If you just want something that reflects it's UTC - 6, DateTimeOffset is what you should use.
Ok thanks, that does the trick. Appreciate the help!
Just don't forget about daylight savings if applicable. As in, if figuring out a future date at specific local time, it might swap from Eastern Standard to Eastern Daylight.
And then handle cases like Arizona not even having DST.
This is why you should just use UTC everywhere. Getting timezones right is nearly impossible.
UTC whenever possible 💯 , but yeah, I understand there's instances where a backend needs to calculate/use local times, like banking.
If you can get away with the client doing local time conversions, that's always the ideal.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.