Oscar Reyes
Oscar Reyes

Reputation: 4352

How to get DateTime from UTC offset

I am trying to figure out how to get a DateTime instance using an UTC offset value, i have to get the DateTime of the desired UTC offset regardless of which UTC the current system is.

All posts i've seen are about getting the UTC string data from a DateTime, also i've seen that there is a post that says how to get the other DateTime by calculating the difference using the current DateTime, that doesn't seem to work well since i need the code to be working regardless of which UTC the system is using.

What i have tried this far:

public static void Main(string[] args) {
    DateTime utcDateTime = DateTime.UtcNow;
    TimeSpan offSet = TimeSpan.FromHours((double)-4.00); // UTC-4
    DateTime newDateTime = utcDateTime.Add(offSet);

    Console.WriteLine(newDateTime);
}

This is something i saw in a different post but it looks that it only changes the hour in a wrong way.. please help.

Upvotes: 0

Views: 5930

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241930

To get the current time in a particular offset (such as UTC-4), as a DateTime, the easiest way (IMHO) is:

DateTime dt = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(-4)).DateTime;

Another (messier) way to get the same results would be:

DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.AddHours(-4), DateTimeKind.Unspecified);

One might also just keep it as a DateTimeOffset, such that the offset from UTC is not lost.

DateTimeOffset dto = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(-4));

Or the messier way:

TimeSpan offset = TimeSpan.FromHours(-4);
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Add(offset), DateTimeKind.Unspecified);
DateTimeOffset dto = new DateTimeOffset(dt, offset);

However, in most cases, one is probably not working with a fixed offset, but is instead looking for the time in a particular time zone, which could be in a variety of different offsets depending on the date in question, due to both daylight saving time and changes in the standard time observed by a particular government.

See also "Time Zone != Offset" in the timezone tag wiki.

In .NET, the TimeZoneInfo class can manage such changes for you. On Windows, it uses Microsoft time zone identifiers, and on Linux or Mac OSX, it uses IANA time zone identifiers. For example:

// On Windows:
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime dt = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);

// On Linux/OSX:
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
DateTime dt = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);

In both examples, the correct UTC offset (either UTC-5 for EST or UTC-4 for EDT) will be applied.

These could also be written using DateTimeOffset values:

// On Windows:
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTimeOffset dto = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tz);

// On Linux/OSX:
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
DateTimeOffset dto = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tz);

Additionally, if you need to write code that can run on either platform, you can use my TimeZoneConverter library to work with either set of identifiers on any platform.

Upvotes: 5

shingo
shingo

Reputation: 27424

Why all of you give so complex answers?

Is this wrong?

DateTime.UtcNow.AddHours(-4); //UTC-4

Upvotes: 1

Related Questions