7

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?

Blorgbeard
  • 97,316
  • 47
  • 222
  • 267
Endy Tjahjono
  • 23,432
  • 23
  • 79
  • 119

2 Answers2

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
  • 2
    Thanks! 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