Unix时间戳(Unix timestamp)转换
当前时间
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" //自定义日期格式 let time = dateformatter.stringFromDate(date) print("对应时间:"+time) |
let timeInterval:NSTimeInterval =NSTimeInterval(timeStamp/1000) let date = NSDate(timeIntervalSince1970: timeInterval) let dateformatter = NSDateFormatter() dateformatter.dateFormat ="yyyy年MM月dd日 HH:mm:ss" //自定义日期格式 let time = dateformatter.stringFromDate(date) print("对应时间:"+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]); |