⏰ 时间戳在线转换工具
当前时间戳
0
加载中...
📅 时间戳转日期
转换结果:
🕐 日期转时间戳
转换结果:
秒:
毫秒:
秒:
毫秒:
⚙️ 批量转换
转换结果:
💻 各编程语言时间戳转换函数
PHP 时间戳转换
// 获取当前时间戳 $timestamp = time(); // 秒 $timestamp_ms = round(microtime(true) * 1000); // 毫秒 // 时间戳转日期时间 $date = date('Y-m-d H:i:s', $timestamp); $date_full = date('Y-m-d H:i:s 星期l', $timestamp); // 日期时间转时间戳 $timestamp = strtotime('2024-01-01 12:00:00'); // DateTime 对象方式 $dt = new DateTime('2024-01-01 12:00:00'); $timestamp = $dt->getTimestamp(); // 格式化输出 echo date('Y年m月d日 H:i:s', time()); echo gmdate('Y-m-d H:i:s', $timestamp); // UTC时间 // 时区设置 date_default_timezone_set('Asia/Shanghai'); $beijing_time = date('Y-m-d H:i:s', $timestamp);
Python 时间戳转换
import time from datetime import datetime, timezone, timedelta # 获取当前时间戳 timestamp_s = int(time.time()) # 秒 timestamp_ms = int(time.time() * 1000) # 毫秒 # 时间戳转日期时间 dt = datetime.fromtimestamp(timestamp_s) date_str = dt.strftime('%Y-%m-%d %H:%M:%S') # 日期时间转时间戳 dt = datetime.strptime('2024-01-01 12:00:00', '%Y-%m-%d %H:%M:%S') timestamp = int(dt.timestamp()) # UTC 时间 utc_dt = datetime.utcfromtimestamp(timestamp_s) utc_str = utc_dt.strftime('%Y-%m-%d %H:%M:%S UTC') # 带时区转换 (北京时间) tz_beijing = timezone(timedelta(hours=8)) dt_beijing = datetime.fromtimestamp(timestamp_s, tz=tz_beijing) # ISO 格式 iso_str = dt.isoformat() print(f"时间戳: {timestamp_s}") print(f"日期: {date_str}")
Java 时间戳转换
import java.time.*; import java.time.format.DateTimeFormatter; // 获取当前时间戳 long timestamp = System.currentTimeMillis(); // 毫秒 long timestampSec = timestamp / 1000; // 秒 // 时间戳转日期时间 (Java 8+) LocalDateTime dt = LocalDateTime.ofEpochSecond(timestampSec, 0, ZoneOffset.ofHours(8)); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String dateStr = dt.format(formatter); // 日期时间转时间戳 LocalDateTime dt = LocalDateTime.parse("2024-01-01 12:00:00", formatter); long timestamp = dt.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); // Instant 方式 Instant instant = Instant.now(); Instant instantFromSec = Instant.ofEpochSecond(timestampSec); // 传统 Date 方式 Date date = new Date(timestamp); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = sdf.format(date); // 时区转换 ZonedDateTime zonedDT = instant.atZone(ZoneId.of("Asia/Shanghai"));
C/C++ 时间戳转换
#include <ctime> #include <chrono> #include <iomanip> #include <sstream> #include <iostream> // 获取当前时间戳 time_t timestamp = time(nullptr); // 秒 auto now = std::chrono::system_clock::now(); auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( now.time_since_epoch() ).count(); // 时间戳转日期时间 time_t ts = 1609459200; struct tm* tm_info = localtime(&ts); char buffer[256]; strftime(buffer, 256, "%Y-%m-%d %H:%M:%S", tm_info); std::cout << buffer << std::endl; // 日期时间转时间戳 struct tm tm = {}; tm.tm_year = 2024 - 1900; tm.tm_mon = 0; // 0-11 tm.tm_mday = 1; tm.tm_hour = 12; tm.tm_min = 0; tm.tm_sec = 0; time_t timestamp = mktime(&tm); // C++20 chrono auto tp = std::chrono::system_clock::now(); auto tt = std::chrono::system_clock::to_time_t(tp);
Go 语言时间戳转换
package main import ( "fmt" "time" ) func main() { // 获取当前时间戳 timestamp := time.Now().Unix() // 秒 timestampMs := time.Now().UnixNano() / 1e6 // 毫秒 // 时间戳转日期时间 t := time.Unix(timestamp, 0) dateStr := t.Format("2006-01-02 15:04:05") // 日期时间转时间戳 t, _ := time.ParseInLocation( "2006-01-02 15:04:05", "2024-01-01 12:00:00", time.Local ) timestamp := t.Unix() // UTC 时间 utcTime := time.Unix(timestamp, 0).UTC() // 指定时区 (北京时间) loc, _ := time.LoadLocation("Asia/Shanghai") beijingTime := time.Unix(timestamp, 0).In(loc) fmt.Printf("时间戳: %d\n", timestamp) fmt.Printf("日期: %s\n", dateStr) fmt.Printf("北京时间: %s\n", beijingTime) }
JavaScript 时间戳转换
// 获取当前时间戳 const timestamp = Date.now(); // 毫秒 const timestampSec = Math.floor(Date.now() / 1000); // 秒 // 时间戳转日期时间 const date = new Date(timestamp); const dateStr = date.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', weekday: 'long' }); // 日期时间转时间戳 const date = new Date('2024-01-01 12:00:00'); const timestamp = date.getTime(); // 毫秒 const timestampSec = Math.floor(date.getTime() / 1000); // 秒 // 自定义格式化 function formatDate(date) { const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, '0'); const d = String(date.getDate()).padStart(2, '0'); const h = String(date.getHours()).padStart(2, '0'); const min = String(date.getMinutes()).padStart(2, '0'); const s = String(date.getSeconds()).padStart(2, '0'); return `${y}-${m}-${d} ${h}:${min}:${s}`; } // UTC 时间 const utcStr = date.toUTCString(); const isoStr = date.toISOString();