How to specify a datetime object as a Central Standard Time or other timezones not UTC in C#?

I am developing a web application where User calls to customers and Customer may be in different country so I need to consider timezone also. So I have two input controls like "Next FollowUp DateTime" and "Next Followup Time Zone".

So user will ask to customers that what is the next follow up datetime and TimeZone. Let's say for example NextFollowUpDateTime is "21/3/2017 10:40PM" and the "Next Followup TimeZone" is CST(Central Standard Time)

So while saving to database I want to convert this to selected TimeZone, let's say CST. I am not able to specify the datetime object as CST.

Following I have tried:

DateTime dateTime = DateTime.SpecifyKind(NextFollowupDatetime, DateTimeKind.Utc);
return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime,”Central Standard Time”);

Here I am able to specify datetime as UTC so it can be converted into another timezones. But I want to specify for other timezones also. Just my intention is to convert the selected datetime into selected timezone (CST or other) then convert it to UTC then save it to database.

Jon Skeet
people
quotationmark

DateTime itself only has three kinds:

  • Unspecified (no specific time zone)
  • UTC
  • Local (system-local)

It doesn't have the notion of being in a specific non-UTC, non-local time zone.

If you want to convert from one time zone to another, you could call TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) and pass in a DateTime with an Unspecified kind.

However, I would personally - and I'm rather biased here - suggest using my Noda Time library instead of using the BCL types.

That way you can create a ZonedDateTime which "knows" it's in the original time zone, and then use ZonedDateTime.InZone to create a new value which "knows" it's in the other time zone. Your code will be a lot clearer in terms of which values are in a specific zone vs which are "local" in a general sense (i.e. not anchored to any time zone) as they'd be LocalDateTime values.

people

See more on this question at Stackoverflow