Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Long type datetime to DateTime with correct time zone

For example 1297380023295 should be 2010/2/11 9 AM I use this code right now

        long dateNumber = num;         long beginTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;         DateTime dateValue = new DateTime(beginTicks + dateNumber * 10000);          return dateValue; 

The result of this function is 1 AM,It is GMT. What can I do with it?

like image 766
Shisoft Avatar asked Feb 11 '11 01:02

Shisoft


People also ask

How do I convert DateTime to specific timezone?

DateTime currentTime = TimeZoneInfo. ConvertTime(DateTime. Now, TimeZoneInfo. FindSystemTimeZoneById("Central Standard Time"));

Does DateTime have timezone C#?

DateTime itself contains no real timezone information.


2 Answers

You're looking for the ToLocalTime() method:

long unixDate = 1297380023295; DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime date= start.AddMilliseconds(unixDate).ToLocalTime(); 
like image 193
SLaks Avatar answered Sep 24 '22 02:09

SLaks


long a= 634792557112051692; //a= ticks time   DateTime dt = new DateTime(a);    Response.Write(dt.Hour.ToString());   //dt.hour convert time ticks to time hour 
like image 32
alireza Avatar answered Sep 24 '22 02:09

alireza