我的项目中有一个奇怪的问题。我试图动态插入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
答案 0 :(得分:2)
在您的示例中,from
是逗号分隔的字符串,而不是Date
构造函数作为参数所需的一系列谨慎变量:
var fromtime = new Date(fromYear, fromMonth, fromDay, fromHour, fromMinute);
(月份为0,因此您可能需要添加1)
答案 1 :(得分:1)
如果您要以
的格式创建日期new Date(year, month, day, hours, minutes, seconds, milliseconds)
您应该直接传递参数而不是连接它们,如此
new Date(fromYear, fromMonth, fromDay, fromHour, fromMinute, 0)
答案 2 :(得分:0)
你在做什么
var fromtime = new Date('2012,8,25,9,22');
而不是
var fromtime = new Date(2012,8,25,9,22);
你需要做的是使用你拥有的日期,如果它在一个字符串中清除了日期的不同部分
新日期(fromYear,fromMonth,fromDay,fromHour,fromMinute,0)
我不认为JS会允许你这样做
你给它一个参数而不是你想要的五个参数。