时间戳转换

开发辅助本地计算,不上传

Unix 时间戳与日期时间在线互转:实时显示当前时间戳,支持秒/毫秒互转、时间戳转日期、日期转时间戳,附多语言取时间戳代码示例。

--

简介

时间戳是指格林威治时间自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数。通俗的讲,时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。

时间单位

单位缩写含义
Y年(Year)
M月(Month)
W周(Week)
D日(Day)
H小时(Hour)
m分钟(Minute)
s秒(Second)

示例

例如:1988年04月26日 08时03分28秒,中国标准时间(GMT+8)对应的时间戳就是 576489808,每秒 +1;換算成毫秒即 576489808000。时间戳根据精度不同,可用 10 位(秒)、13 位(毫秒)、16 位(微秒)等长度表示。

ISO 8601 标准格式:如 2022-06-26T18:23:11Z(Z 表示 UTC)或 2022-06-26T18:23:11+08:00(东八区)。程序开发中建议存库与传输统一使用时间戳或 ISO 8601,避免时区歧义。

各语言获取/转换时间戳代码示例(点击展开)
JavaScript
// 当前时间戳(秒)
Math.floor(new Date().getTime() / 1000)  // 或 Date.now() / 1000
// 时间戳 → 日期
new Date(1799332322 * 1000)
Java
// 当前时间戳(毫秒)
System.currentTimeMillis();
// 秒
System.currentTimeMillis() / 1000;
// 时间戳 → 日期
new Date(1799332322L * 1000);
PHP
// 当前时间戳(秒)
time();
// 毫秒
round(microtime(true) * 1000);
// 时间戳 → 日期
date('Y-m-d H:i:s', 1799332322);
Python
import time
# 当前时间戳(秒,浮点)
time.time()
# 秒(整型)
int(time.time())
# 时间戳 → 日期
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1799332322))
Go
import "time"
// 当前时间戳(秒)
time.Now().Unix()
// 毫秒
time.Now().UnixNano() / 1e6
// 时间戳 → 日期
time.Unix(1799332322, 0)
MySQL
-- 当前时间戳(秒)
SELECT UNIX_TIMESTAMP();
-- 时间戳 → 日期
SELECT FROM_UNIXTIME(1799332322);
Shell
# 当前时间戳(秒)
date +%s
# 时间戳 → 日期
date -d @1799332322 '+%Y-%m-%d %H:%M:%S'
C#
// 当前时间戳(秒)
(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000
// 时间戳 → 日期
DateTimeOffset.FromUnixTimeSeconds(1799332322).LocalDateTime
C
#include <time.h>
// 当前时间戳(秒)
time(NULL);
// 时间戳 → 日期
struct tm *t = localtime(&(time_t){1799332322});
Swift
// 当前时间戳(秒)
NSDate().timeIntervalSince1970
// 时间戳 → 日期
Date(timeIntervalSince1970: 1799332322)
Dart
// 当前时间戳(毫秒)
DateTime.now().millisecondsSinceEpoch
// 时间戳 → 日期
DateTime.fromMillisecondsSinceEpoch(1799332322000)
Ruby
# 当前时间戳(秒)
Time.now.to_i
# 时间戳 → 日期
Time.at(1799332322)