在Node.js日期对象中设置日期将日期显示为其他值 [英] Setting date in nodejs date object shows date as different value
问题描述
当我使用以下语句设置日期对象时,nodejs中的输出显示不同。您能帮我理解为什么会这样吗?以及我需要如何传递才能打印正确的值。
When i'm setting up date object using the below statement the output showing different in nodejs. Can you please help me to understand why it is like this. And how i need to pass to print right value.
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打印为==> 2017-01-31T18:30:00.000Z
2017-01-01 is printed as ==> 2017-01-31T18:30:00.000Z
2017-01-31打印为==> 2017 -03-02T18: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()
迄今为止的对象。
Printing your logs like this
console.log("2017-01-01 is printed as ==>"+date1);
console.log("2017-01-31 is printed as ==>"+date2);
will give you your expected logs
It is because console log will take date as object of date while using comma and while using +
it will apply object.toString()
to date object.
这篇关于在Node.js日期对象中设置日期将日期显示为其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!