Nodejs Date()“小时”与getHours()不同

时间:2019-11-21 06:29:39

标签: javascript date datetime

有人知道为什么dateTime返回的“ hh”与下面的time返回的“ hh”不同吗?

//Create new Date() Object
var dateTime = new Date();
console.log(dateTime);

//Extract Date
var date = dateTime.getDate() + "/" + (parseInt(dateTime.getMonth())+1) + "/" + dateTime.getFullYear().toString().substr(-2);
console.log(date);

//Extract Time
var time = (dateTime.getHours()) + ":" + (parseInt(dateTime.getMinutes())+1) + ":" + (parseInt(dateTime.getSeconds())+1);
console.log(time);

1 个答案:

答案 0 :(得分:1)

好得多,非常感谢您的建议!

//Create new Date() Object
var dateTime = new Date();
console.log(dateTime);
//Extract Date
var date = dateTime.getDate() + "/" + (parseInt(dateTime.getMonth())+1) + "/" + dateTime.getFullYear().toString().substr(-2);
console.log(date);

//Extract Time
var time = (dateTime.getHours()) + ":" + dateTime.getMinutes() + ":" + dateTime.getSeconds();
console.log(time);

输出: 2019-11-21T08:26:54.781Z 19/11/11 10:26:54