10

I have this UTC+0 Date :

2011-11-28T07:21:41.000Z

and I'd like, on C#, convert it to a PST Date. How can I do it? Tried with :

object.Data.ToLocalTime()

but I can't get the correct value (which should be 2011-11-27)

EDIT

Also tried (after suggesion on another topic) this :

DateTime convertedDate = DateTime.SpecifyKind(
    DateTime.Parse(object.Data.ToShortDateString()),
    DateTimeKind.Utc);                    

DateTime dt = convertedDate.ToLocalTime();
string dataVideo = dt.ToShortDateString();

but the date still 28/11/2011, not 27/11/2011

Community
  • 1
  • 1
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • Check this question: http://stackoverflow.com/questions/179940/c-sharp-convert-utc-gmt-time-to-local-time – Nayan Dec 09 '11 at 08:28
  • Tried some strategies, such as `DateTime.Parse(object.Data.ToShortDateString()).ToLocalTime()` but nothing happens... – markzzz Dec 09 '11 at 08:39
  • I don't see a failure mode but one: check that your machine is actually in the PST timezone. Document the time you get, not just the date. – Hans Passant Dec 09 '11 at 09:27
  • So, what Time Zone is set on your computer? The suggested by @Stijn solution should work. – Petr Abdulin Dec 09 '11 at 09:30

2 Answers2

16

I've changed my clock to use UTC-08:00 Pacific Time.

DateTime timestamp = DateTime.Parse("2011-11-28T07:21:41.000Z");
Console.WriteLine("UTC: " + timestamp.ToUniversalTime());
Console.WriteLine("PST: " + timestamp.ToLocalTime());

Output:

UTC: 28/11/2011 7:21:41
PST: 27/11/2011 23:21:41

Example with TimeZoneInfo

DateTime timestamp = DateTime.Parse("2011-11-28T07:21:41.000Z");
Console.WriteLine("UTC: " + timestamp.ToUniversalTime());
Console.WriteLine("GMT+1: " + timestamp.ToLocalTime());
Console.WriteLine("PST: " + TimeZoneInfo.ConvertTimeBySystemTimeZoneId(timestamp, "Pacific Standard Time"));

Output:

UTC: 28/11/2011 7:21:41
GMT+1: 28/11/2011 8:21:41
PST: 27/11/2011 23:21:41
user247702
  • 23,641
  • 15
  • 110
  • 157
  • First, I can't drop the Z (I won't to get a string replace on a Date, that's horrible). Second, no it doesnt do the trick : the date still 28/11/2011, which, on PST, is 27/11/2011 (12 hour before). – markzzz Dec 09 '11 at 09:20
  • 1
    Nevermind dropping the Z. I've updated the answer. PST is UTC-08:00 as far as I can see, not UTC-12:00 ? – user247702 Dec 09 '11 at 09:27
  • :O! But I need to print that value without change clock of my computer :) Is there a method to convert a certain date in UTC (my date) to PST date?. Yeah, sorry, is UTC-08:00, UTC-12:00 should be PDT... – markzzz Dec 09 '11 at 09:31
  • 3
    @markzzz yes, you need to use TimeZoneInfo class, but it's only available int .NET 3.5+. – Petr Abdulin Dec 09 '11 at 09:34
  • Yes, I have .NET 3.5. How can I do it with TimeZoneInfo? Can you give to me an example? – markzzz Dec 09 '11 at 09:36
  • More info can be found in the link below. Using local time won't work well if the application is hosted on a server outside of the intended time zone. https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones – DavidShepard Apr 30 '20 at 17:44
4

For a little more color

2011-11-28T07:21:41.000Z

This is a ISO8601 Timestamp, the Z at the end means UTC time. This represents a specific instant in time.

DateTime.Parse will return to you a local date time structure, there are three types of datetime kinds, UTC, Local, and Unspecified.

If you try displaying this, it will show you this instant in your computers current timezone (I'm eastern time so when I print it I get 11/28/2011 2:21:41 AM).

If I want to switch this DateTime Kind to UTC, I could do something like

DateTime.Parse("2011-11-28T07:21:41.000Z").ToUniversalTime()

Printing this now (since it's kind is now UTC) I get 11/28/2011 7:21:41 AM.

Note that although the time is printed differently both these date times are referring to the same instant in time.

To display this instant in a different timezone, the easiest way imo is the TimeZoneInfo class (though I'm not sure it's 100% accurate).

TimeZoneInfo.ConverTimeBySystemTimeZoneId(dateTime, "Pacific Standard Time").

Printing it now will yield your desired result 11/27/2011 11:21:41 PM

Note that this return DateTime's Kind property is now Unspecified, meaning you won't be able to transfer it back to UTC without more information. You no longer have a specific instant in time, rather you have a unspecified time..we know it's the same instant as the ones previously just in pacific time, but the computer no longer knows that. Keep that in mind if you want to store this time.

Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68