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

后端 未结 4 924
栀梦
栀梦 2020-12-15 16:53

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

        long dateNumber = num;
        long beginTicks = new DateTi         


        
相关标签:
4条回答
  • 2020-12-15 17:06

    Powershell script piece, just FYI

    $minDate = New-Object "System.DateTime"
    $minDate = $minDate.AddYears(1969)
    $minDate.AddMilliseconds(1446616420947)
    
    0 讨论(0)
  • 2020-12-15 17:13
    long a= 634792557112051692;
    //a= ticks time
      DateTime dt = new DateTime(a);
       Response.Write(dt.Hour.ToString());
    
    
    //dt.hour convert time ticks to time hour
    
    0 讨论(0)
  • 2020-12-15 17:13

    You can specify the DateTimeKind when you create a new DateTime object, so you could specify that as UTC and then use .ToLocalTime to convert it to local time:

            long dateNumber = 1297380023295;
            long beginTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
    
            DateTime dt = new DateTime(beginTicks + dateNumber * 10000, DateTimeKind.Utc);
            MessageBox.Show(dt.ToLocalTime().ToString());
    
    0 讨论(0)
  • 2020-12-15 17:22

    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();
    
    0 讨论(0)
提交回复
热议问题