时间戳与时间格式转换

时间戳简介

JavaScript时间戳总毫秒数(长度13),Unix时间戳是总秒数(长度10)。

格林威治时间 UTC: 1970年01月01日00时00分00秒
北京时间 Local:1970年01月01日08时00分00秒

JavaScript时间戳转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var date = new Date();
console.log(date);

// 第一种
time1 = date.getTime();
console.log(time1)

// 第二种
time2 = date.valueOf();
console.log(time2)

// 第三种
time3 = Date.parse(date);
console.log(time3)


var date1 = new Date(time1);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
console.log(Y+M+D+h+m+s);

// 输出:
// 第一,第二种更精确,第三种只精确到秒
/*
Tue Apr 23 2019 17:27:21 GMT+0800 (中国标准时间)
1556011641157
1556011641157
1556011641000
2019-04-23 17:27:21
*/

Date()参数形式有7种

1
2
3
4
5
6
7
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date("yyyy/MM/dd hh:mm:ss");
new Date("yyyy/MM/dd");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);

CSharp时间戳转换

Unix时间戳转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// <summary>
/// DateTime转换为Unix时间戳
/// </summary>
/// <returns></returns>
public static long GetUnixTimeStamp()
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long timeStamp = (long)(TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow) - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}

/// <summary>
/// 获取本地时间的Unix时间戳
/// </summary>
/// <param name="time">本地时间</param>
/// var dt = new DateTime(2019, 4, 30, 15, 30, 00, DateTimeKind.Local);
/// <returns></returns>
public static long GetUnixTimeStamp(DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long timeStamp = (long)(time - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}

/// <summary>
/// Unix时间戳转换为DateTime
/// </summary>
/// <returns></returns>
public static DateTime GetDateTimeByUnix(long timeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
DateTime dt = startTime.AddSeconds(timeStamp);
return dt;
}

// 调用
var timeStamp1= GetUnixTimeStamp();
Console.WriteLine(timeStamp1);
var dt1 = GetDateTimeByUnix(timeStamp1);
System.Console.WriteLine(dt1.ToString("yyyy/MM/dd HH:mm:ss"));

JavaScript时间戳转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <summary>
/// DateTime转换为JavaScript时间戳
/// </summary>
/// <returns></returns>
public static long GetJavaScriptTimeStamp()
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long timeStamp = (long)(TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow) - startTime).TotalMilliseconds; // 相差毫秒数
return timeStamp;
}

/// <summary>
/// JavaScript时间戳转换为DateTime
/// </summary>
/// <returns></returns>
public static DateTime GetDateTimeByJavaScript(long timeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
DateTime dt = startTime.AddMilliseconds(timeStamp);
return dt;
}

// 调用
var timeStamp = GetJavaScriptTimeStamp();
Console.WriteLine(timeStamp);
var dt = GetDateTimeByJavaScript(timeStamp);
System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss.fff"));

参考:

How to deserialize a unix timestamp (μs) to a DateTime from JSON?

JavaScript Date 对象

C# DateTime与时间戳转换

JavaScript在线测试