I need to check whether a certain time zone is still within a specified date. Something like DateTime.Today == DateTime.Parse("2016-06-30")
but for certain time zone. What is the best way to do it?
You need to get UTC Time
, find TimeZoneInfo
, and convert UTC time
to your TimeZoneInfo
.
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo serverZone = TimeZoneInfo.FindSystemTimeZoneById(YourTimeZoneID);
DateTime currentDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, serverZone);
I would check out the TimeZoneInfo
class.
Method for converting the to a specific timezone is:
public static DateTime ConvertTime(
DateTime dateTime,
TimeZoneInfo sourceTimeZone,
TimeZoneInfo destinationTimeZone
)
There are other methods for dealing with things like UTC as well. Check out the documentation here.
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