JavaScript获取时间戳的坑

JavaScript获取时间戳的坑JavaScript 获取时间戳的三个方法 Date parse newDate 获取 13 位时间戳 例如 00 newDate valueOf 获取 13 位时间戳 精确到毫秒 例如 49newDate getTime 获取 13 位时间戳 精确到毫秒 例如 25 示例以上面第一种方法为例 lt

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

  • Date.parse(new Date())
    获取13位时间戳,例如00,

  • (new Date()).valueOf()
    获取13位时间戳,精确到毫秒,例如:49

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

示例

以上面第一种方法为例

 

timestamp

上述页面中的脚本执行后,会返回13位数字,如:49。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得到的时间戳不准确。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223474.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午2:07
下一篇 2026年3月17日 下午2:07


相关推荐

  • pycharm安装Translation翻译插件

    pycharm安装Translation翻译插件打开 pycharmfile gt settings gt plugins gt marketplace gt 搜索 Translation 设置翻译工具的唤醒快捷键打开 pycharmfile gt settings gt Keymap gt 搜索 Translation 可以选择翻译的工具打开 pycharmfile gt settings gt tools gt Translation 进入 pycharm 按快捷键呼出翻译界面 输入内容 按回车键

    2026年3月27日
    3
  • 破解不加微信看朋友圈「建议收藏」

    破解不加微信看朋友圈「建议收藏」有网友发邮件问,用空间万能查看器天涯,可以强制看非好友的朋友圈?今天小编就给大家说说微信朋友圈的一些事情。微信好友之间可以看到对方朋友圈每次发的一些动态记录,只要对方不设置让你看不到,就可以看到。不是微信好友想看对方朋友圈,也是可以理解的,毕竟微信朋友圈,和QQ空间设置了这个权限是需要权限才能进去的,有的网友有方法进别人设置了权限的QQ空间,那么微信的权限朋友圈其实和QQ权限空间原理是一样的。QQ空间和微信朋友圈其实是一样的意思,只是表现的形势不同,但是有的人喜欢上微信,有的人喜欢上QQ,不管你

    2022年4月29日
    2.8K
  • jQuery下载和安装详细教程[通俗易懂]

    jQuery下载和安装详细教程[通俗易懂]下载jQuery我们可以到jQuery的官网下载jQuery文件(PS:其实jQuery就是一个封装了很多函数的js文件,把这个js文件导入到网页中就可以了)。jQuery官网地址:https://jquery.com/打开官网,即可看到jQuery的下载按钮,点击进入下载页面。有两个版本的jQuery可以下载:Productionversion-用于实际的网站中,是已经被精简和…

    2022年5月3日
    450
  • 怎么用vscode运行java_捷达vs9参数配置

    怎么用vscode运行java_捷达vs9参数配置首先,当然是先下载VSCode官方链接:https://code.visualstudio.comVSCode(VisualStudioCode)官网选择匹配自己的操作系统版本就好(Windows,macOS,Linux),敢说自己是「Codeediting.Redefined.」,看得出来VSCode的底气。VSCode三大特点:免费、开源、跨平台下载运行后,会看到如下的Welcome「欢迎」界面。【VSCode】的颜值和【Github】夜间版都属一流

    2022年10月3日
    2
  • 钉钉喊话企业:技术文档可以给,API免费调用,但建议不装“龙虾”丨AI“养虾”冷热对谈

    钉钉喊话企业:技术文档可以给,API免费调用,但建议不装“龙虾”丨AI“养虾”冷热对谈

    2026年3月12日
    2
  • dga 分析_tcga数据库分析

    dga 分析_tcga数据库分析02n-0iy6gn3ozzwmyu.7i43n9qil1g1z2-.com0e527eaf_5ec5_4623_9fe9_e459583acd72.com0fmgm1cuu7h1279dghgka0ltg.com0gqo9jx0ir0rjy4b.com0hm4mqw9hoe3gvajwi.com-0j2zkzul4p5v8zo4d0m.127f5zlwuhq7yy2qufp6l.com…

    2025年8月31日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号