JavaScript获取时间戳的三个方法:

  • Date.parse(new Date())
    获取13位时间戳,例如1553581293000,
  • (new Date()).valueOf()
    获取13位时间戳,精确到毫秒,例如:1553581461549
  • new Date().getTime()。
    获取13位时间戳,精确到毫秒,例如:1553581482325

示例

以上面第一种方法为例

<!DOCTYPE html>
<html><body><p id="demo">timestamp</p><button type="button" onclick="myFunction()">点击这里,显示时间戳</button><script>function myFunction() {document.getElementById("demo").innerHTML = Date.parse(new Date());}</script></body></html>

上述页面中的脚本执行后,会返回13位数字,如:1553581461549。parse方法会解析包含日期的字符串,并返回该日期与1970年1月1日午夜之间的毫秒数(UTC时间)。而上面parse解析的字符串是通过new Date()方法来获取到的,从下面的内容来看,new Date()获取的是一个当前主机的本地时间。

/** Enables basic storage and retrieval of dates and times. */
interface Date {/** Returns a string representation of a date. The format of the string depends on the locale. */toString(): string;/** Returns a date as a string value. */toDateString(): string;/** Returns a time as a string value. */toTimeString(): string;/** Returns a value as a string value appropriate to the host environment's current locale. */toLocaleString(): string;/** Returns a date as a string value appropriate to the host environment's current locale. */toLocaleDateString(): string;/** Returns a time as a string value appropriate to the host environment's current locale. */toLocaleTimeString(): string;/** Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. */valueOf(): number;/** Gets the time value in milliseconds. */getTime(): number;/** Gets the year, using local time. */getFullYear(): number;/** Gets the year using Universal Coordinated Time (UTC). */getUTCFullYear(): number;/** Gets the month, using local time. */getMonth(): number;/** Gets the month of a Date object using Universal Coordinated Time (UTC). */getUTCMonth(): number;/** Gets the day-of-the-month, using local time. */getDate(): number;/** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */getUTCDate(): number;/** Gets the day of the week, using local time. */getDay(): number;/** Gets the day of the week using Universal Coordinated Time (UTC). */getUTCDay(): number;/** Gets the hours in a date, using local time. */getHours(): number;/** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */getUTCHours(): number;/** Gets the minutes of a Date object, using local time. */getMinutes(): number;/** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */getUTCMinutes(): number;/** Gets the seconds of a Date object, using local time. */getSeconds(): number;/** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */getUTCSeconds(): number;/** Gets the milliseconds of a Date, using local time. */getMilliseconds(): number;/** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */getUTCMilliseconds(): number;/** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */getTimezoneOffset(): number;/*** Sets the date and time value in the Date object.* @param time A numeric value representing the number of elapsed milliseconds since midnight, January 1, 1970 GMT.*/setTime(time: number): number;/*** Sets the milliseconds value in the Date object using local time.* @param ms A numeric value equal to the millisecond value.*/setMilliseconds(ms: number): number;/*** Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).* @param ms A numeric value equal to the millisecond value.*/setUTCMilliseconds(ms: number): number;/*** Sets the seconds value in the Date object using local time.* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setSeconds(sec: number, ms?: number): number;/*** Sets the seconds value in the Date object using Universal Coordinated Time (UTC).* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setUTCSeconds(sec: number, ms?: number): number;/*** Sets the minutes value in the Date object using local time.* @param min A numeric value equal to the minutes value.* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setMinutes(min: number, sec?: number, ms?: number): number;/*** Sets the minutes value in the Date object using Universal Coordinated Time (UTC).* @param min A numeric value equal to the minutes value.* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setUTCMinutes(min: number, sec?: number, ms?: number): number;/*** Sets the hour value in the Date object using local time.* @param hours A numeric value equal to the hours value.* @param min A numeric value equal to the minutes value.* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setHours(hours: number, min?: number, sec?: number, ms?: number): number;/*** Sets the hours value in the Date object using Universal Coordinated Time (UTC).* @param hours A numeric value equal to the hours value.* @param min A numeric value equal to the minutes value.* @param sec A numeric value equal to the seconds value.* @param ms A numeric value equal to the milliseconds value.*/setUTCHours(hours: number, min?: number, sec?: number, ms?: number): number;/*** Sets the numeric day-of-the-month value of the Date object using local time.* @param date A numeric value equal to the day of the month.*/setDate(date: number): number;/*** Sets the numeric day of the month in the Date object using Universal Coordinated Time (UTC).* @param date A numeric value equal to the day of the month.*/setUTCDate(date: number): number;/*** Sets the month value in the Date object using local time.* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.* @param date A numeric value representing the day of the month. If this value is not supplied, the value from a call to the getDate method is used.*/setMonth(month: number, date?: number): number;/*** Sets the month value in the Date object using Universal Coordinated Time (UTC).* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.* @param date A numeric value representing the day of the month. If it is not supplied, the value from a call to the getUTCDate method is used.*/setUTCMonth(month: number, date?: number): number;/*** Sets the year of the Date object using local time.* @param year A numeric value for the year.* @param month A zero-based numeric value for the month (0 for January, 11 for December). Must be specified if numDate is specified.* @param date A numeric value equal for the day of the month.*/setFullYear(year: number, month?: number, date?: number): number;/*** Sets the year value in the Date object using Universal Coordinated Time (UTC).* @param year A numeric value equal to the year.* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively. Must be supplied if numDate is supplied.* @param date A numeric value equal to the day of the month.*/setUTCFullYear(year: number, month?: number, date?: number): number;/** Returns a date converted to a string using Universal Coordinated Time (UTC). */toUTCString(): string;/** Returns a date as a string value in ISO format. */toISOString(): string;/** Used by the JSON.stringify method to enable the transformation of an object's data for JavaScript Object Notation (JSON) serialization. */toJSON(key?: any): string;
}interface DateConstructor {new(): Date;new(value: number | string): Date;new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;(): string;readonly prototype: Date;/*** Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.* @param s A date string*/parse(s: string): number;/*** Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.* @param month The month as an number between 0 and 11 (January to December).* @param date The date as an number between 1 and 31.* @param hours Must be supplied if minutes is supplied. An number from 0 to 23 (midnight to 11pm) that specifies the hour.* @param minutes Must be supplied if seconds is supplied. An number from 0 to 59 that specifies the minutes.* @param seconds Must be supplied if milliseconds is supplied. An number from 0 to 59 that specifies the seconds.* @param ms An number from 0 to 999 that specifies the milliseconds.*/UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;now(): number;
}

问题来了,一般服务器的时间是固定的,但是客户端且分布世界各地,客户端系统设置的时间也不一定按标准时区分布。因此就会造成通过JavaScript得到的时间戳不准确。

JavaScript获取时间戳的坑相关推荐

  1. JavaScript获取时间戳与时间戳转化

    Javascript 获取当前时间戳(毫秒级别): 第一种方法: var timestamp1 = Date.parse( new Date()); 结果:1470220594000 第二种方法: v ...

  2. JavaScript获取时间戳

    JavaScript 获取当前时间戳: 第一种方法: var timestamp = Date.parse(new Date()); 结果:1513156754000 第二种方法: var times ...

  3. js/javascript获取时间戳的5种方法

    1.获取时间戳精确到秒,13位 const timestamp = Date.parse(new Date()); console.log(timestamp);//输出 1591669256000 ...

  4. JavaScript(获取时间戳)

    获取时间戳 1.时间日期对象.getTime() 2.在new前面放+: var date = +new Date() console.log(date); 将+后面的字符串转成数字 var a = ...

  5. JavaScript 获取时间戳的几种方式

    1.Date.now()获取当前时间的时间戳 Date.now() 2.getTime()方法 new Date().getTime() 3.Date.parse(dateString)方法 注意:这 ...

  6. 如何在JavaScript中获取时间戳

    如何在JavaScript中获取时间戳 +运算符 我们可以使用+运算符将日期对象直接转换为UNIX时间戳. 例如,我们可以这样写: +new Date() +日期对象之前操作者触发valueOf的方法 ...

  7. html实现获取电脑时间戳,JavaScript 获取当前时间戳

    JavaScript 获取当前时间戳: 第一种方法: var timestamp = Date.parse(new Date()); 结果:1280977330000 第二种方法: var times ...

  8. Javascript获取当前时间戳的方法

    原文地址为: Javascript获取当前时间戳的方法 定义日期: Date 对象用于处理日期和时间. 可以通过 new 关键词来定义 Date 对象.以下代码定义了名为 myDate 的 Date ...

  9. php js 获取当前时间戳,JavaScript 获取当前时间戳的代码_时间日期

    JavaScript 获取当前时间戳: 第一种方法: var timestamp = Date.parse(new Date()); 结果:1280977330000 第二种方法: var times ...

最新文章

  1. Python 第三方库自动安装脚本
  2. ssh免密码登录的原理
  3. 20189216 2018-2019-2 《密码与安全新技术专题》第七次作业
  4. linux c 进程编程,linux c/c++ 编程之-----进程操作
  5. c++的lambda表达式捕获this_贯穿 C++ 11 与 C++ 17 的 Lambda 到底是个什么?
  6. 关于用C#编写ActiveX控件2(转)
  7. list集合根据jsonobjectvalue排序_Java之List集合两种排序的性能比较
  8. Atitit 数据join 的原理与java实现 Atitit join表连接的原理与实现 13、SQL Server 表连接的三种方式   (1) Merge Join   (2) Nested
  9. 蓝桥杯 C语言 试题 历届试题 格子刷油漆
  10. 计算机一级ms office选择题题库,全国计算机等级考试一级MSOFFICE选择题题库.doc
  11. JVM高性能调优宝典【包含VisualVM工具下载安装教程】持续更新优化
  12. Exchange2010安装配置
  13. Linux下安装docker与kubernetes(k8s)
  14. 2019春招宇视科技嵌入式面试
  15. the transaction flow in v1.0 of Hyperledger Fabric(fabric1.0交易流程)
  16. 【信号与系统学习笔记】—— 拉普拉斯反变换+由零极点图对傅里叶变换几何求值
  17. 【环境安装】ubuntu18.04利用opam安装指定版本的coq工作环境
  18. Dynamics 365 CRM证书更换
  19. 自媒体原创文章多平台发布操作方法教程!
  20. Uedit32比较2个文件的内容

热门文章

  1. 微信小程序实现登陆功能
  2. JavaScript数据类型基本数据类型与引用数据类型的区别
  3. 【随机过程】马尔可夫链(1)
  4. 用R进行文本分析初探——以《红楼梦》为例
  5. WXML和HTML的区别
  6. Data truncation: Incorrect datetime value: ‘XXXX‘
  7. java 向word中添加excel附件并向excel单元格中加入图片并压缩图片并根据图片动态控制单元格高度宽度
  8. 【Python】浅谈 字节码 + 虚拟机 (Python 解释器)
  9. java操作跨页的word cell_Java 创建Word表格/嵌套表格、添加/复制表格行或列、设置表格跨页断行...
  10. hql 字符串where语句_常用的HQL语句