当我使用以下语句设置日期对象时,输出在 nodejs 中显示不同。你能帮我理解为什么会这样吗?以及我需要如何传递以打印正确的值。
var date1= new Date(2017,01,01);
var date2= new Date(2017,01,31);
console.log("2017-01-01 is printed as ==>",date1);
console.log("2017-01-31 is printed as ==>",date2);
输出
2017-01-01 is printed as ==> 2017-01-31T18:30:00.000Z
2017-01-31 is printed as ==> 2017-03-02T18:30:00.000Z
最佳答案
像这样打印你的日志
console.log("2017-01-01 打印为 ==>"+date1);
console.log("2017-01-31 打印为 ==>"+date2);
会给你你预期的日志
这是因为控制台日志在使用逗号时将日期作为日期的对象,而在使用 +
时它将应用 object.toString()
到日期对象。
关于javascript - 在 nodejs 日期对象中设置日期将日期显示为不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047000/