npm install --save dates-range
var dateRange = require('dates-range');
var dates = dateRange(new Date('January 1, 2000'), new Date('January 11 2016'));
for(var day of dates){
console.log(day + ' quarter ' + day.quarter);
console.log(day.month + ' '+day.day + ' ' + day.year);
}
Each iteration of the loop produces a CurrentDay
object.
- day (Day of the month.)
- year
- month (Month object)
- dayOfWeek (DayOfWeek object)
- dayOfYear
- quarter (The Financial Quarter)
- iso (The ISO date string)
- date (The instance of Date)
An instance of CurrentDay
in a string context returns the same as:
day.month + ' ' + day.day + ', ' + day.year;
- number
- name (The English name of the month)
- short (The abbreviation for the month)
dates.month
returns an object, but in string contexts returns the month name.
var dates = dateRange(new Date('January 1, 2000'), new Date('January 11 2016'));
for(var day of dates){
//day.month.toString gets called printing the month name
console.log(day.month + ' '+day.day + ' ' + day.year);
}
- number
- name (The English name of the day of the week.)
- short (The abbreviation for the day of the week.)
dates.dayOfWeek
returns an object, but in string contexts returns the day of week name.
var dates = dateRange(new Date('January 1, 2000'), new Date('January 11 2016'));
for(var day of dates){
//print the day of the week
console.log(day.dayOfWeek + ' ');
}
The object returned from calling require('dates-range')()
is truly just for iteration.
Really you can't do anything else with it. Maybe you can call dates.next
, or mash together a larger iterator with a generator function using yield dates
.
Getting info with properties can be fast as the getters are synchronously lazy calling internal Date
methods only when you need them.
The for of
syntax is standard, but only supported by a few environments with, or without flags. Use at your own risk.
Happy coding. :)