JavaScript日期-设置UTC月份 [英] Javascript date - setting UTC month

查看:37
本文介绍了JavaScript日期-设置UTC月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我进行了自动化测试,想知道为什么我在某些日期特定的东西上出错了.

Today i ran my automates tests and wondered why I got an error about some date specific stuff.

事实证明,设置固定UTC月份不再有效.但是昨天做到了.我知道没有任何变化.

Turns out that setting a fixed UTC month is not working anymore. But yesterday it did. There are no changes I am aware of.

我尝试运行以下代码

var d = new Date();
d.setUTCMonth(1);
d.toISOString();

返回

"2019-03-01T10:28:42.108Z"

但是月份显然应该是2月.还有为什么将日期设置为01而不是今天(29)

But month should obviously be february. Also why is the day set to 01 and not today (29)

在Chrome,Edge,Firefox上进行了测试.

Tested on Chrome, Edge, Firefox.

有什么建议吗?难道我做错了什么?库中有错误吗?

Any advice? Am I doing something wrong? Is there a bug in the library?

推荐答案

哼...有趣的错误.

尝试一下:

var d = new Date('2019-03-10T00:00:00');
d.setUTCMonth(1);
d.toISOString();

这是因为今天是月末,您正在初始化到今天的日期.

It's because today is end of month and you're initializing the date to today.

这篇关于JavaScript日期-设置UTC月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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