简体   繁体   English

Date().setDate()不一致的行为

[英]Date() .setDate() inconsistent behaviour

According to 根据

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

I should expect that setDate() in code 我应该期待代码中的setDate()

var now = new Date();
now.setDate(0);

will change now to the last day of the previous month. 现在将更改为上个月的最后一天。

It means I should be able to do something like this: 这意味着我应该能够执行以下操作:

now.setDate(0).setDate(1)

to get the date of the first day of the previous month. 获取上个月第一天的日期。

But id doesn't work like that. 但是id不能那样工作。

var now = new Date();
console.log(now)
// FF 24: Date {Wed Jul 09 2014 16:35:49 GMT+0100 (IST)}

now.setDate(0);
console.log(now)
// FF 24: Date {Mon Jun 30 2014 16:35:49 GMT+0100 (IST)}

But

var now = new Date().setDate(0);
console.log(now)
// 1404142784241

Question: 题:

What is difference between 之间有什么区别

var now = new Date();
now.setDate(0);

and

var now = new Date().setDate(0);

In the second example, you're looking at the return value of the .setDate() function, and not the date itself. 在第二个示例中,您正在查看.setDate()函数的返回值 ,而不是日期本身。 The .setDate() function returns the timestamp value corresponding to the updated value of the object. .setDate()函数返回与该对象的更新值相对应的时间戳值。 It's like doing this: 就像这样:

var now = new Date();
now.setDate(0);
console.log(now.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM