Created
April 7, 2019 19:32
-
-
Save kudinovfedor/362c60ba2a094a8ab115a6940be8cbd5 to your computer and use it in GitHub Desktop.
Get date range depending on the set number of days
This file contains 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
/** | |
* Get date range | |
* | |
* @example | |
* getDateRange(30); | |
* | |
* @param {number} [days=1] - Number of days | |
* @returns {{start: string, end: string}} | |
*/ | |
const getDateRange = (days = 1) => { | |
const daysTimestamp = days * 24 * 60 * 60 * 1000; | |
const timestampEnd = new Date().getTime(); | |
const timestampStart = timestampEnd - daysTimestamp; | |
const start = new Date(timestampStart).toISOString().substr(0, 10).replace(/-/g, ''); | |
const end = new Date(timestampEnd).toISOString().substr(0, 10).replace(/-/g, ''); | |
return { | |
start: start, | |
end: end | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment