C#时间戳转换

时间戳转DateTime

timestamp为10位秒级* 10000000,若为13位毫秒级*10000。

private DateTime TimestampToDateTime(long timestamp)

{

    DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));

    long lTime = timestamp * 10000000;

    TimeSpan nowTimeSpan = new TimeSpan(lTime);

    DateTime resultDateTime = dateTimeStart.Add(nowTimeSpan);

    return resultDateTime;

}

DateTime转时间戳

秒级

private int DateTimeToTimestamp(DateTime time)
{
    DateTime startDateTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(197, 1, 1));
    return Convert.ToInt32((time - startDateTime).TotalSeconds);

}

 

 

posted @ 2017-06-01 11:19  Schauspieler  阅读(450)  评论(0编辑  收藏  举报