为什么新的Date()在Javascript中减去1天? [英] Why is new Date() subtracting 1 day in Javascript?

查看:471
本文介绍了为什么新的Date()在Javascript中减去1天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将String转换为Date对象。

I need to convert a String to a Date object.

日期字符串按以下格式提供:

The date string is delivered in the following format:


2015-01-28T00:00:00

"2015-01-28T00:00:00"

当我创建一个新的日期,我得到上一个日期:

When I create a new Date, I get the previous date:

Entered: new Date("2015-01-28T00:00:00")

结果:2015年1月27日星期二17:00:00 GMT-0700(山标准时间)

有谁知道为什么会这样?

Does anyone know why this is occurring?

推荐答案

当您输入以下内容时:

 new Date("2015-01-28T00:00:00");
 // Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)

浏览器假定您在GMT时区中提供日期。因此它会自动将给定日期转换为您当地的日期。

the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.

最好通知浏览器您正在处理的时区,以防止将来出现问题:

It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:

 new Date("2015-01-28T00:00:00-07:00");
 // Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)

这篇关于为什么新的Date()在Javascript中减去1天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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