Last active
January 6, 2019 14:11
-
-
Save Andy-set-studio/c4437190b24b105500b96f2fb20b7242 to your computer and use it in GitHub Desktop.
π₯ A little JavaScript snippet here for if you want to modify a `Date` object and get back a Unix timestamp in seconds based on that modification.
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
/** | |
* Take a base date, modify it with passed options and return back the number of seconds since Epoch time | |
* | |
* @param {Object} [options={}] Options for what you want to modify, follows the following format: { hours: 0, minutes: 0, seconds: 0 } | |
* @returns {Number} | |
*/ | |
const modifyUnixTime = (options = {}) => { | |
// Create our modifications object by merging a static base with the passed options | |
const modifications = Object.assign( | |
{ hours: 0, minutes: 0, seconds: 0 }, | |
options | |
); | |
const dateBase = new Date(); | |
// Run through each option and modify the time accordingly | |
dateBase.setHours(dateBase.getHours() + modifications.hours); | |
dateBase.setMinutes(dateBase.getMinutes() + modifications.minutes); | |
dateBase.setSeconds(dateBase.getSeconds() + modifications.seconds); | |
return Math.round(dateBase.getTime() / 1000); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment