显示 javascript 中两个日期范围之间的所有日期
Show all date between two date range in javascript
我想显示两个之间的所有日期 days.I 做不到 this.Please 帮助..
这是我的代码...
var Day = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date("2017-04-10");
var secondDate = new Date("2017-04-15");
var diffDays = (secondDate.getTime() - firstDate.getTime())/Day;
alert(diffDays);
for(var i=0;i<= diffDays ;i++)
{
var d = new Date(firstDate + i);
alert(d);
}
您的 i
变量是要显示的日期。您的 firstDate
变量是 Date
.
这一行:
var d = new Date(firstDate + i);
将这些加在一起并尝试创建一个新的 Date
。由于它们是不同的类型(日期和数字),类型强制开始起作用(即:日期和数字被转换为字符串,并连接在一起)。
试试这个:
var DAY_IN_MS = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var theDate = new Date("2017-04-10");
var i = 5;
// Date + Number = Type Coercion! Both objects will be converted to strings and concatenated together.
alert(theDate + i);
// The Date constructor doesn't care, and will work because it can get a date from the first part of the string.
alert(new Date(theDate + i));
// If you use `getTime()` you can get the numerical value of the Date, which you can use for arithmetic.
alert(theDate.getTime());
// By adding `i`, however, you are only adding a few milliseconds to the first date !!
alert(new Date(theDate.getTime() + i);
// Number + Number of days times milliseconds per day = success!
alert(new Date(theDate.getTime() + (i * DAY_IN_MS)));
我想你的意思是:
var d = new Date(firstDate.getTime() + (i * Day));
简短回答,将正确的毫秒数增加到 firstDate.getTime()
:
for(var i=0;i<= diffDays ;i++)
{
firstDate = new Date(firstDate.getTime() + Day);
console.log(firstDate);
}
长答案,你的代码有几处错误:
此处 var d = new Date(firstDate + i);
您要将 i
的值添加到 Date 对象字符串表示形式,在本例中,该值在循环中递增。
然后您将此字符串解析为新的 Date 对象,但 Javascript 仅识别日期并忽略附加数字
您应该像获取 diffDays
一样尝试获取 firstDate
的毫秒数,然后添加 i * Day
(也可以考虑将此变量重命名为其他名称,例如 const DAY_IN_MS
之类的)。
我想显示两个之间的所有日期 days.I 做不到 this.Please 帮助.. 这是我的代码...
var Day = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date("2017-04-10");
var secondDate = new Date("2017-04-15");
var diffDays = (secondDate.getTime() - firstDate.getTime())/Day;
alert(diffDays);
for(var i=0;i<= diffDays ;i++)
{
var d = new Date(firstDate + i);
alert(d);
}
您的 i
变量是要显示的日期。您的 firstDate
变量是 Date
.
这一行:
var d = new Date(firstDate + i);
将这些加在一起并尝试创建一个新的 Date
。由于它们是不同的类型(日期和数字),类型强制开始起作用(即:日期和数字被转换为字符串,并连接在一起)。
试试这个:
var DAY_IN_MS = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var theDate = new Date("2017-04-10");
var i = 5;
// Date + Number = Type Coercion! Both objects will be converted to strings and concatenated together.
alert(theDate + i);
// The Date constructor doesn't care, and will work because it can get a date from the first part of the string.
alert(new Date(theDate + i));
// If you use `getTime()` you can get the numerical value of the Date, which you can use for arithmetic.
alert(theDate.getTime());
// By adding `i`, however, you are only adding a few milliseconds to the first date !!
alert(new Date(theDate.getTime() + i);
// Number + Number of days times milliseconds per day = success!
alert(new Date(theDate.getTime() + (i * DAY_IN_MS)));
我想你的意思是:
var d = new Date(firstDate.getTime() + (i * Day));
简短回答,将正确的毫秒数增加到 firstDate.getTime()
:
for(var i=0;i<= diffDays ;i++)
{
firstDate = new Date(firstDate.getTime() + Day);
console.log(firstDate);
}
长答案,你的代码有几处错误:
此处
var d = new Date(firstDate + i);
您要将i
的值添加到 Date 对象字符串表示形式,在本例中,该值在循环中递增。然后您将此字符串解析为新的 Date 对象,但 Javascript 仅识别日期并忽略附加数字
您应该像获取 diffDays
一样尝试获取 firstDate
的毫秒数,然后添加 i * Day
(也可以考虑将此变量重命名为其他名称,例如 const DAY_IN_MS
之类的)。