新的日期不能给我合适的时间

2022-03-26 00:00:00 datetime slice date javascript
startDate = "2019-03-07 (목) 12:00";

var year = startDate.slice(0,4);
var month = startDate.slice(5,7);
var day = startDate.slice(8,10);
var hour = startDate.slice(15,17);
var minute = startDate.slice(18,20);

var selEndDatetime = new Date(year, month, day, hour, minute);
console.log(selEndDatetime);

我想查看"Tue Mar 07 2019 12:00:00 GMT+0900(한국표준시)",但控制台显示消息"Sun Apr 07 2019 12:00:00 GMT+0900(한국표준시)"。

此代码有什么问题?如何修改它以获得所需的输出?


解决方案

月份从0开始。创建Date实例时,需要将月份从字符串解析为整数和减1。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
startDate = "2019-03-07 (목) 12:00";

var year = startDate.slice(0,4);
var month = parseInt(startDate.slice(5,7))
var day = startDate.slice(8,10);
var hour = startDate.slice(15,17);
var minute = startDate.slice(18,20);

var selEndDatetime = new Date(year, month - 1, day, hour, minute);
console.log(selEndDatetime);

相关文章