gpt4 book ai didi

javascript - 新日期无法给我正确的时间

转载 作者:行者123 更新时间:2023-11-28 14:21:33 25 4
gpt4 key购买 nike

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开始。创建日期实例时需要将月份从字符串解析为整数并减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);

关于javascript - 新日期无法给我正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55035275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com