Timestamp Converter
Unix timestamp and datetime mutual conversion with multiple timezone support
Current Time
9/1/2025, 12:55:42 PM
Unix Timestamp: 1756702542 (Seconds)
1756702542033 (Milliseconds)
JavaScript
JavaScript中处理时间戳的常用方法
// 获取当前时间戳(毫秒)
const timestamp = Date.now();
// 获取当前时间戳(秒)
const timestampSeconds = Math.floor(Date.now() / 1000);
// 时间戳转日期
const date = new Date(timestamp);
// 日期转时间戳
const timestampFromDate = date.getTime();
Python
Python中使用time和datetime模块处理时间戳
import time
from datetime import datetime
# 获取当前时间戳(秒)
timestamp = int(time.time())
# 获取当前时间戳(毫秒)
timestamp_ms = int(time.time() * 1000)
# 时间戳转日期
date = datetime.fromtimestamp(timestamp)
# 日期转时间戳
timestamp_from_date = int(date.timestamp())
PHP
PHP中处理时间戳的函数
<?php
// 获取当前时间戳(秒)
$timestamp = time();
// 获取当前时间戳(毫秒)
$timestamp_ms = round(microtime(true) * 1000);
// 时间戳转日期
$date = date("Y-m-d H:i:s", $timestamp);
// 日期转时间戳
$timestamp_from_date = strtotime($date);
?>
Java
Java 8+ 中使用新的时间API处理时间戳
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
// 获取当前时间戳(毫秒)
long timestamp = System.currentTimeMillis();
// 获取当前时间戳(秒)
long timestampSeconds = Instant.now().getEpochSecond();
// 时间戳转日期
LocalDateTime date = LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault()
);
// 日期转时间戳
long timestampFromDate = date.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli();
MySQL
MySQL中处理时间戳的SQL函数
-- 获取当前时间戳(秒)
SELECT UNIX_TIMESTAMP();
-- 获取当前时间戳(毫秒)
SELECT UNIX_TIMESTAMP(NOW(3)) * 1000;
-- 时间戳转日期
SELECT FROM_UNIXTIME(1704067200);
-- 日期转时间戳
SELECT UNIX_TIMESTAMP("2024-01-01 12:00:00");
Go
Go语言中使用time包处理时间戳
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间戳(秒)
timestamp := time.Now().Unix()
// 获取当前时间戳(毫秒)
timestampMs := time.Now().UnixMilli()
// 时间戳转日期
date := time.Unix(timestamp, 0)
// 日期转时间戳
timestampFromDate := date.Unix()
}
C#
C#中使用DateTimeOffset处理时间戳
using System;
// 获取当前时间戳(秒)
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// 获取当前时间戳(毫秒)
long timestampMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// 时间戳转日期
DateTime date = DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime;
// 日期转时间戳
long timestampFromDate = new DateTimeOffset(date).ToUnixTimeSeconds();
Ruby
Ruby中使用Time类处理时间戳
# 获取当前时间戳(秒)
timestamp = Time.now.to_i
# 获取当前时间戳(毫秒)
timestamp_ms = (Time.now.to_f * 1000).to_i
# 时间戳转日期
date = Time.at(timestamp)
# 日期转时间戳
timestamp_from_date = date.to_i
Swift
Swift中使用Date类处理时间戳
import Foundation
// 获取当前时间戳(秒)
let timestamp = Int(Date().timeIntervalSince1970)
// 获取当前时间戳(毫秒)
let timestampMs = Int(Date().timeIntervalSince1970 * 1000)
// 时间戳转日期
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
// 日期转时间戳
let timestampFromDate = Int(date.timeIntervalSince1970)
Kotlin
Kotlin中使用Java时间API处理时间戳
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
// 获取当前时间戳(毫秒)
val timestamp = System.currentTimeMillis()
// 获取当前时间戳(秒)
val timestampSeconds = Instant.now().epochSecond
// 时间戳转日期
val date = LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault()
)
// 日期转时间戳
val timestampFromDate = date.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()
Unix timestamp is the number of seconds since January 1, 1970 00:00:00 UTC
Milliseconds timestamp is the number of milliseconds since January 1, 1970 00:00:00 UTC
Timezone is the time offset relative to UTC
Different programming languages and systems use different time formats
推荐工具
根据使用统计为您推荐的热门工具