时间戳转换工具

快速转换时间戳与日期时间格式

返回首页

当前时间戳

1761807109
单个转换
批量转换

时间戳 → 日期时间

日期时间 → 时间戳

简介

时间戳,是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数(不考虑闰秒),用于表示一个时间点。然而,这种格式对于人类阅读并不友好,因此需要转换成可读的日期和时间格式。这个工具能够将时间戳快速转换为人类可读的日期时间格式,同时也支持反向转换,即将日期时间转换为时间戳。

获取当前时间戳

JavaScript
// 秒级时间戳
Math.floor(Date.now() / 1000)

// 毫秒级时间戳
Date.now()
Python
import time
import datetime

# 秒级时间戳
int(time.time())

# 毫秒级时间戳
int(time.time() * 1000)

# 使用datetime
int(datetime.datetime.now().timestamp())
Java
// 秒级时间戳
System.currentTimeMillis() / 1000

// 毫秒级时间戳
System.currentTimeMillis()

// 使用Instant (Java 8+)
Instant.now().getEpochSecond()  // 秒
Instant.now().toEpochMilli()    // 毫秒
PHP
// 秒级时间戳
time()

// 毫秒级时间戳
(int)(microtime(true) * 1000)
.NET/C#
// 秒级时间戳
(long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds

// 毫秒级时间戳
(long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds

// .NET 6+
DateTimeOffset.UtcNow.ToUnixTimeSeconds()
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
Go
import "time"

// 秒级时间戳
time.Now().Unix()

// 毫秒级时间戳
time.Now().UnixMilli()

// 纳秒级时间戳
time.Now().UnixNano()
Swift
// 秒级时间戳
Int(Date().timeIntervalSince1970)

// 毫秒级时间戳
Int(Date().timeIntervalSince1970 * 1000)

// 使用Foundation
Int(NSDate().timeIntervalSince1970)
Objective-C
// 秒级时间戳
(long)[[NSDate date] timeIntervalSince1970]

// 毫秒级时间戳
(long)([[NSDate date] timeIntervalSince1970] * 1000)
Ruby
# 秒级时间戳
Time.now.to_i

# 毫秒级时间戳
(Time.now.to_f * 1000).to_i

# 使用strftime
Time.now.strftime('%s').to_i
Shell
# 秒级时间戳
date +%s

# 毫秒级时间戳 (GNU date)
date +%s%3N

# macOS
date +%s
C
#include <time.h>
#include <sys/time.h>

// 秒级时间戳
time_t timestamp = time(NULL);

// 毫秒级时间戳
struct timeval tv;
gettimeofday(&tv, NULL);
long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
Groovy
// 秒级时间戳
System.currentTimeMillis() / 1000

// 毫秒级时间戳
System.currentTimeMillis()

// 使用Date
new Date().time
Lua
-- 秒级时间戳
os.time()

-- 毫秒级时间戳 (Lua 5.4+)
math.floor(os.clock() * 1000)

-- 使用socket (LuaSocket)
require "socket"
math.floor(socket.gettime() * 1000)
Dart
// 秒级时间戳
DateTime.now().millisecondsSinceEpoch ~/ 1000

// 毫秒级时间戳
DateTime.now().millisecondsSinceEpoch

// 微秒级时间戳
DateTime.now().microsecondsSinceEpoch
Erlang
% 秒级时间戳
erlang:system_time(second)

% 毫秒级时间戳
erlang:system_time(millisecond)

% 使用calendar
calendar:datetime_to_gregorian_seconds(calendar:universal_time()) - 62167219200
MySQL
-- 秒级时间戳
SELECT UNIX_TIMESTAMP();

-- 指定日期的时间戳
SELECT UNIX_TIMESTAMP('2024-01-01 00:00:00');

-- 当前时间
SELECT UNIX_TIMESTAMP(NOW());
SQLite
-- 秒级时间戳
SELECT strftime('%s', 'now');

-- 指定日期的时间戳
SELECT strftime('%s', '2024-01-01 00:00:00');

-- 毫秒级 (SQLite 3.9.0+)
SELECT (julianday('now') - 2440587.5) * 86400000;