Last active
September 7, 2017 04:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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