0

I need to create a DateTime object with a fixed value on a given TimeZone for today, lets assume it to be 2018-04-23 now I would do DateTime.Today or DateTime.Now.Date to get it on the local timezone, or I could do DateTime.UtcNow.Date to get it on UTC. after that if I want the 9am its something like DateTime.Today.AddHours(9) and that would give me 2018-04-23 09:00 am but that would be, again, on the local timezone, as would new DateTime(.....).

The question then is how could I get 2018-04-23 09:00 am on a given timezone, lets say PST for instance?

Luiso
  • 4,173
  • 2
  • 37
  • 60
  • 1
    Relevant: [DateTime vs. DateTimeOffset](https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset) – John Wu Apr 23 '18 at 20:19
  • @Luiso Let me know if the "duplicate" is totally unsuitable for some reason. – 15ee8f99-57ff-4f92-890c-b56153 Apr 23 '18 at 20:20
  • @EdPlunkett I saw that question/answer before posting my question, and, at least, as far as I could see, it tells you how to create a `DateTime` with timezone information, I want to know how to create any arbitrary `DateTime` value for any given timezone. Please correct me if I'm wrong – Luiso Apr 23 '18 at 20:41
  • `DateTime` doesn't hold any time zone information. It only tracks a `DateTimeKind` in its `Kind` property. If you want "now in some other time zone", then take `DateTime.UtcNow` and convert it using the `TimeZoneInfo.Convert...` methods. Otherwise, please clarify what you're looking to do. – Matt Johnson-Pint Apr 23 '18 at 21:50
  • I found a solution, I got the idea from the Jon Skeet answer to the question above. I needed to have `Today at 9:30 am` no matter the timezone so I created a `DateTime` with the right time, and then `DateTime.SpecifyKind(myDate, unspecified)` and that did the trick. Any other way to go about this? – Luiso Apr 24 '18 at 14:07
  • Keep in mind that "today" may be different in different time zones at any given moment. You will need to do something like `TimeZoneInfo.ConvertTime(DateTime.UtcNow, someTimeZone).Date` to get the base date (instead of `DateTime.Today`). – Matt Johnson-Pint Apr 24 '18 at 17:41

0 Answers0