Unix時間戳轉換

當前時間
Unix時間戳

本地時間

什麼是Unix時間戳?

UNIX時間,或稱POSIX時間是UNIX或類UNIX系統使用的時間表示方式:從1970年1月1日0時0分0秒起至現在的總秒數,不考慮閏秒。

在編程語言中獲取Unix時間戳:

語言 毫秒
JavaScript Math.round(new Date() / 1000) new Date().getTime()
Java System.currentTimeMillis() / 1000 System.currentTimeMillis()
Python int(time.time()) int(time.time() * 1000)
Go time.Now().Unix() time.Now().UnixNano() / 1e6
PHP time() (int)(microtime(true) * 1000)
Ruby Time.now.to_i (Time.now.to_f * 1000).to_i
C# DateTimeOffset.UtcNow.ToUnixTimeSeconds() DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
Swift NSDate().timeIntervalSince1970 NSDate().timeIntervalSince1970 * 1000
Objective-C [[NSDate date] timeIntervalSince1970] [[NSDate date] timeIntervalSince1970] * 1000
Unix/Linux Shell date +%s date +%s000

不同編程語言中實現Unix時間戳(Unix timestamp)轉普通時間

語言 毫秒
JavaScript var unixTimestamp = new Date(Unix timestamp * 1000)
commonTime = unixTimestamp.toLocaleString()
var unixTimestamp = new Date(Unix timestamp)
commonTime = unixTimestamp.toLocaleString()
Java String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp * 1000)) String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp))
Python time.gmtime(Unix timestamp) time.gmtime(Unix timestamp/1000)
Go time.Unix(Unix timestamp, 0).Format("2006-01-02 15:04:05") time.Unix(Unix timestamp/1000, 0).Format("2006-01-02 15:04:05")
PHP date('r', Unix timestamp) date('r', Unix timestamp/1000)
Ruby Time.at(Unix timestamp) Time.at(Unix timestamp/1000)
C# DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime newDateTime = converted.AddSeconds(Unix timestamp);
newDateTime.ToLocalTime();
DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime newDateTime = converted.AddSeconds(Unix timestamp/1000);
newDateTime.ToLocalTime();
Swift let timeInterval:NSTimeInterval =NSTimeInterval(timeStamp)
let date = NSDate(timeIntervalSince1970: timeInterval)
let dateformatter = NSDateFormatter()
dateformatter.dateFormat ="yyyy-MM-dd HH:mm:ss" //format
let time = dateformatter.stringFromDate(date)
print("time is:"+time)
let timeInterval:NSTimeInterval =NSTimeInterval(timeStamp/1000)
let date = NSDate(timeIntervalSince1970: timeInterval)
let dateformatter = NSDateFormatter()
dateformatter.dateFormat ="yyyy-MM-dd HH:mm:ss" //format
let time = dateformatter.stringFromDate(date)
print("time is:"+time)
Objective-C NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
Nslog("%@",[formatter stringFromDate:date]);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
Nslog("%@",[formatter stringFromDate:date]);