Skip to content

Instantly share code, notes, and snippets.

@aflansburg
Last active September 7, 2017 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflansburg/c09557d6271ddd6382d39553c6ebb39b to your computer and use it in GitHub Desktop.
Save aflansburg/c09557d6271ddd6382d39553c6ebb39b to your computer and use it in GitHub Desktop.
Provided a number of days, pretty print a date range in MMDDYYYY format.
// pretty print a date range MMDDYYYY
// argument is the number of days for the date range
console.log(printDateRange(365));
function printDateRange(days){
let options = {timeZone: 'America/Chicago'} // set timeZone using IANA tz
let curr = new Date(), prev = new Date();
prev.setDate((prev.getDate() - days));
prev = prev.toLocaleString('en-US', options);
curr.setDate(curr.getDate());
curr = curr.toLocaleString('en-US', options);
const regex = /.*(?=,)/g;
let match = [prev, curr].map(i=>{
let m = regex.exec(i);
regex.lastIndex = 0;
return m[0]
}
);
return match[0] + ' - ' + match[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment