Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTC+0 Date to PST Date?

Tags:

c#

datetime

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

like image 557
markzzz Avatar asked Dec 09 '11 08:12

markzzz


1 Answers

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
like image 103
user247702 Avatar answered Oct 02 '22 21:10

user247702