Skip to content

Instantly share code, notes, and snippets.

@amanmeghrajani
Created August 23, 2018 19:33
Show Gist options
  • Save amanmeghrajani/c2e4c392de42031ba259503903e33a25 to your computer and use it in GitHub Desktop.
Save amanmeghrajani/c2e4c392de42031ba259503903e33a25 to your computer and use it in GitHub Desktop.
// Formatted YYYYMMDDHHMMSS Int for timestamp calculation without needing any extra libs
const getFormattedDateTime = (dateObj) => {
let verifyDoubleDigits = (vals) => vals.map(val => val.length === 1 ? "0" + val : val)
let date = dateObj && dateObj.getMonth === 'function' ? datObj : new Date()
let year = date.getFullYear() .toString()
let month = (date.getMonth()+1).toString()
let day = date.getDate() .toString()
let hour = date.getHours() .toString()
let minutes = date.getMinutes() .toString()
let seconds = date.getSeconds() .toString()
let formatted = year
verifyDoubleDigits([month, day, hour, minutes, seconds]).forEach(val => formatted = formatted + val)
return parseInt(formatted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment