使用JavaScript的一致的客户端日期/时间戳(考虑TimeZones) [英] Consistent Client Side Date/timestamp using JavaScript(considering TimeZones)

查看:61
本文介绍了使用JavaScript的一致的客户端日期/时间戳(考虑TimeZones)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于手册中有关JavaScript中日期的引用:

My question is regarding this quote from the manual about dates in JavaScript:


注意:使用Date构造函数解析日期字符串(和
Date.parse(它们等效),由于
浏览器的差异和不一致而强烈反对。

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

用户的新日期('2016-04-14')输出为 Wed Apr 13 2016 17:00:00 GMT-0700(US Mountain标准时间),他不得不使用 .toUTCString()

new Date('2016-04-14') output for a user was Wed Apr 13 2016 17:00:00 GMT-0700 (US Mountain Standard Time) upon which he had to use .toUTCString().

如何

推荐答案

new Date()。getTime,该如何处理呢? (); 返回一个整数值,作为自 1970年1月1日以来客户端计算机上的时间。

new Date().getTime(); returns an integer value as the time on client's machine since 1970 Jan 1.

一个整数值,它将与语言环境,浏览器版本,不同的浏览器(IE,Chrome,Mozilla或任何其他浏览器)无关。

Since this is an integer value, it will be agnostic of locales, browser version, different browsers (IE, Chrome, Mozilla, or anything).

因此,这应该为您提供一致的结果在te只要知道客户端的时区,客户端计算机上的时间均方根值。

So, this should give you consistent results in terms of time on client's machine as long as client's timezone is known.

您可以使用 getTimezoneOffset API

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

这与 new Date()。getTime(); 应该会给您一致的结果。

This together with new Date().getTime(); should give you consistent results.

这篇关于使用JavaScript的一致的客户端日期/时间戳(考虑TimeZones)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆