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 :(得分:3)
月份从0开始。创建日期实例时,您需要将月份从字符串解析为整数并减去1。
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);
答案 1 :(得分:1)
您只需将startDate
字符串作为参数传递给Date即可创建selEndDatetime
代码:
const startDate = '2019-03-07 (목) 12:00';
const selEndDatetime = new Date(startDate);
console.log(selEndDatetime);