比较Javascript和时区中的日期 [英] Comparing dates in Javascript and timezones

查看:78
本文介绍了比较Javascript和时区中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用<,>,=,> =和< =比较Javascript中的日期时,在比较中是否以任何方式使用了时区?我希望时区被忽略。

When comparing dates in Javascript using <, >, =, >= and <= is the timezone used in any way in the comparison? I am hoping that the timezone is ignored.

推荐答案

时间戳的字符串表示形式的时区部分已被考虑在内期望,当您将其转换为JavaScript Date 对象时:内部值为简单标量,已标准化为 UTC 。因此,在比较 Date 对象时,不需要特殊的时区处理:

The timezone part of string representation of a timestamp is taken into account as you would expect, when you convert it into JavaScript Date object: the internal value is a simple scalar, normalized to UTC. So there is no need for special timezone handling when comparing Date objects:

var d1 = new Date(Date.parse("Mon, 25 Dec 1995 13:30:00 +0430"));
var d2 = new Date(Date.parse("Mon, 25 Dec 1995 13:30:00 GMT"));
print("d1:", d1);
print("d2:", d2);
if (d1<d2) {
    print("d1 is less then d2");
} else if (d1>d2) {
    print("d1 is greater then d2");
} else {
    print("d1 equals to d2");
}

给出以下输出:

d1: Mon Dec 25 1995 09:00:00 GMT+0000
d2: Mon Dec 25 1995 13:30:00 GMT+0000
d1 is less then d2

[请参见在线演示]

如果比较时间戳的字符串表示形式,很可能会遇到麻烦。

You'll most probably get into trouble if you compare string representations of time stamps.

这篇关于比较Javascript和时区中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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