Skip to content

Instantly share code, notes, and snippets.

@callblueday
Last active September 22, 2017 02:44
Show Gist options
  • Save callblueday/6209558 to your computer and use it in GitHub Desktop.
Save callblueday/6209558 to your computer and use it in GitHub Desktop.
第一种方法:
var timestamp = Date.parse(new Date());
结果:1280977330000
第二种方法:
var timestamp = (new Date()).valueOf();
结果:1280977330748
以上代码将获取从 1970年1月1日午夜开始的毫秒数。二者的区别是,第一种方法的毫秒位上为全零,即只是精确到秒的毫秒数
如题所示,返回unix时间戳所对应的具体时间:
var time = '1278927966';
// 关键在乘1000,因为时间是相对于1970年开始的,所以乘1000后将会转到当前时间。
var real_time = new Date(time) * 1000;
document.write(real_time);
代码很简单就完成时间戳的转换。
@callblueday
Copy link
Author

Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)
是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
UNIX时间戳的0按照ISO 8601规范为 :1970-01-01T00:00:00Z.
一个小时表示为UNIX时间戳格式为:3600秒;一天表示为UNIX时间戳为86400秒,闰秒不计算。
在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题或Y2038。
时间 秒
1 分钟 60
1 小时 3600
1 天 86400
1 周 604800
1 月 (30.44 天) 2629743
1年 (365.24 天) 31556926

编程中获取Unix时间戳
Math.round(new Date().getTime()/1000)
getTime()返回数值的单位是毫秒

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment