Skip to content

Instantly share code, notes, and snippets.

@Andy-set-studio
Last active January 6, 2019 14:11
Show Gist options
  • Save Andy-set-studio/c4437190b24b105500b96f2fb20b7242 to your computer and use it in GitHub Desktop.
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.
/**
* 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