Last active
October 22, 2022 02:26
-
-
Save vasanthv/de2d57e3fd00b1bfdff1efb0cdbc0adb to your computer and use it in GitHub Desktop.
Format a date string
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
/* | |
This function changes the datestring to the following format | |
Jan 29 2018 - 09:51 AM | |
*/ | |
function formatDate(dateString){ | |
const date = new Date(dateString); | |
const hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours(); | |
return [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ][date.getMonth()] + ' ' | |
+ (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ' | |
+ date.getFullYear() + ' - ' | |
+ (hours < 10 ? '0' + hours : hours) + ':' | |
+ (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ' ' | |
+ (date.getHours() >= 12 ? 'PM' : 'AM'); | |
} | |
console.log(formatDate('2018-01-29 16:21:39.901Z')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment