C # operations timestamp

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011127019/article/details/90715956

 

1. What is the timestamp

JavaScript must first know the difference between Unix timestamp:

JavaScript timestamp: it is GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) until now the total number of milliseconds .

Unix timestamp: is GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) until now the total number of seconds .

JavaScript can be seen that the time stamp of the total number of milliseconds , Unix timestamp is the total number of seconds .

Such as the same is of 2016/11/03 12:30:00, into JavaScript timestamp 1478147400000; 1478147400 convert Unix timestamp.

 

2. JavaScript timestamp conversion

2.1 C # DateTime timestamp converted to JavaScript

DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), TimeZoneInfo.Local);  // 当地时区

long timeStamp = (long)(DateTime.Now - startTime).TotalMilliseconds; // 相差毫秒数

System.Console.WriteLine(timeStamp);

 

2.2 JavaScript timestamps converted to C # DateTime

long jsTimeStamp = 1478169023479;

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区

DateTime dt = startTime.AddMilliseconds(jsTimeStamp);

System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff"));

 

TimeZone use in .NET Core client server time turn prompted local time but the compiler has expired

When our international projects, to deal with the issue time zone.

Before .NET Core we can convert the following code in time for the client server time:

DateTime serverTime = TimeZone.CurrentTimeZone.ToLocalTime(clientTime);

In .NET Core Lane, TimeZone class has been marked as expired, so how should we use the area but the conversion of the API for it?

DateTime serverTime = TimeZoneInfo.ConvertTime(clientTime, TimeZoneInfo.Local);

The following are the bloggers write their own DateTimeExtensions class converted into an extension method server time:

public static class DateTimeExtensions
{
    /// <summary>
    /// 将客户端时间转换为服务端本地时间
    /// </summary>
    /// <param name="clientTime">客户端时间</param>
    /// <returns>返回服务端本地时间</returns>
    public static DateTime ToServerLocalTime(this DateTime clientTime)
    {
        //DateTime serverTime1 = TimeZone.CurrentTimeZone.ToLocalTime(clientTime); //在.NET Core标识已过期的类TimeZone的写法
        DateTime serverTime2 = TimeZoneInfo.ConvertTime(clientTime, TimeZoneInfo.Local);//等价的建议写法
        return serverTime2;
    }
}

 

More:

C # How to get zero time today

C # time string into a date, time of day point determination

 C # in local time, UTC time format, GMT time formatting

Guess you like

Origin blog.csdn.net/u011127019/article/details/90715956