Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Today for a specific time zone

Tags:

date

c#

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?

like image 988
Endy Tjahjono Avatar asked Jul 01 '16 02:07

Endy Tjahjono


2 Answers

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);
like image 76
currarpickt Avatar answered Dec 03 '22 13:12

currarpickt


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.

like image 45
Cubicle.Jockey Avatar answered Dec 03 '22 14:12

Cubicle.Jockey