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?
Asked
Active
Viewed 5,038 times
7
Blorgbeard
- 97,316
- 47
- 222
- 267
Endy Tjahjono
- 23,432
- 23
- 79
- 119
-
[This](http://stackoverflow.com/a/441145/4934172) might help. – 41686d6564 stands w. Palestine Jul 01 '16 at 02:20
2 Answers
4
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);
currarpickt
- 2,255
- 4
- 22
- 36
-
2Thanks! But since what I need is just date, here is what I did: `DateTime theDate = (TimeZoneInfo.ConvertTimeFromUtc(utcTime, serverZone)).Date;` – Endy Tjahjono Jul 01 '16 at 03:19
1
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.
Cubicle.Jockey
- 3,208
- 1
- 18
- 31
-
great, it works ``` DateTime today = TimeZoneInfo.ConvertTime(DateTime.Today, zone);``` – Johann Medina Jul 14 '18 at 00:46