时间戳转换

当前时间戳

时间戳转日期时间

日期时间转时间戳

简介

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

获取当前时间戳
Swift
NSDate().timeIntervalSince1970
Go
import (
  "time"
)
int64(time.Now().Unix())
Java
// pure java
System.currentTimeMillis() / 1000
// joda java
DateTime.now().getMillis() / 1000
// java >= 8
Instant.now().getEpochSecond()
C
#include <sys/time.h>

// ...
struct timeval tv;
gettimeofday(&tv, NULL);
// 秒: tv.tv_sec
// 毫秒: tv.tv_sec * 1000LL + tv.tv_usec / 1000
JavaScript
Math.round(new Date() / 1000)
Objective-C
[[NSDate date] timeIntervalSince1970]
MySQL
SELECT unix_timestamp(now())
SQLite
SELECT strftime('%s', 'now')
Erlang
calendar:datetime_to_gregorian_seconds(calendar:universal_time())-719528*24*3600.
PHP
<?php
// pure php
time();
<?php
// carbon php
use Carbon\Carbon;
Carbon::now()->timestamp;
Python
import time
time.time()
import arrow
arrow.utcnow().timestamp
Ruby
Time.now.to_i
Shell
date +%s
Groovy
(new Date().time / 1000).longValue()
Lua
os.time()
.NET/C#
DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Dart
(new DateTime.now().millisecondsSinceEpoch / 1000).truncate()