JS

HOW TO CREATE A DATE RANGE IN JAVASCRIPT

PARSE DATES INTO UNIX TIMESTAMPS var start = new Date("1 Feb 2003") .getTime(); var end = new Date("28 Feb 2003") .getTime();

START-END DATE RANGE

01

LOOP & CREATE DATE RANGE var range = [];  for (let unix=start; unix<=end; unix += 86400000) { range.push(new Date(unix)); }

7 DAYS STARTING FROM 1 FEB 2003  var dstart = "1 Feb 2003", offset = 7;

LAST/NEXT N DAYS (A)

02

CALCULATE START/END DATES var start = new Date(dstart),        end = new Date(dstart); end.setDate(end.getDate() + offset);

SUBTRACT START DATE IF CALCULATING “LAST N DAYS” start.setDate(start.getDate() - offset);

PARSE INTO UNIX TIMESTAMPS start = start.getTime(); end = end.getTime();

LAST/NEXT N DAYS (B)

03

LOOP & CREATE DATE RANGE var range = []; for (let unix=start; unix<=end; unix += 86400000) { range.push(new Date(unix)); }