gpt4 book ai didi

javascript - 创建日期对象

转载 作者:行者123 更新时间:2023-11-28 20:50:19 27 4
gpt4 key购买 nike

我的项目中有一个奇怪的问题。我正在尝试动态地将参数插入到 Date 对象构造函数中。这是我的代码:

from += fromYear + "," + fromMonth + "," + fromDay + "," + fromHour + "," + fromMinute;
to += toYear + "," + toMonth + "," + toDay + "," + toHour + "," + toMinute;

console.log(from); //here is log value: 2012,8,25,9,22
console.log(to); //another log: 2012,8,25,9,52

//Creating object
var fromtime = new Date(from);
var totime = new Date(to);

当我尝试提醒日期对象(totime 或 fromtime)时,出现错误:日期无效。我不知道如何通过它。你能帮我吗?

我尝试过这个: Creating Date Object JS

最佳答案

在您的示例中,from 是一个逗号分隔的字符串,而不是 Date 构造函数需要的一系列离散变量作为参数:

var fromtime =  new Date(fromYear, fromMonth, fromDay, fromHour, fromMinute);

(月份是从 0 开始的,因此您可能需要添加 1)

关于javascript - 创建日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12580252/

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