Home  >  Article  >  Backend Development  >  Share How to convert timestamp in C#?

Share How to convert timestamp in C#?

零下一度
零下一度Original
2017-06-24 10:44:281685browse

时间戳转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);

}

 

 

The above is the detailed content of Share How to convert timestamp in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn