Last active
October 13, 2018 21:17
-
-
Save mikeerickson/6b03e353df4f1c72dec8868f6fe7f0e4 to your computer and use it in GitHub Desktop.
timestamp.js
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
const formatDate = (date = '', useAMPM = true) => { | |
date = ((date === '') || (date === null)) ? date = new Date() : date; | |
// build time | |
let hours = date.getHours(); | |
let minutes = date.getMinutes(); | |
let seconds = date.getSeconds(); | |
let ampm = ''; | |
hours = hours ? hours : 12; // the hour '0' should be '12' | |
minutes = minutes < 10 ? '0' + minutes : minutes; | |
seconds = seconds < 10 ? '0' + seconds : seconds; | |
if (useAMPM) { | |
ampm = hours >= 12 ? 'PM' : 'AM'; | |
hours = hours % 12; | |
hours = hours < 10 ? '0' + hours : hours; | |
} | |
let strTime = `${hours}:${minutes}:${seconds} ${ampm}`; | |
// build date | |
let month = date.getMonth() + 1; | |
month = month < 10 ? '0' + month : month; | |
let day = date.getDate(); | |
day = day < 10 ? '0' + day : day; | |
let strDate = `${date.getFullYear()}-${month}-${day}`; | |
return `${strDate} ${strTime}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment