Last active
December 9, 2015 20:49
-
-
Save shivanarrthine/4326757 to your computer and use it in GitHub Desktop.
Custom date and time format with JavaScript
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
// Custom Date Format | |
var mydate = new Date("2012-12-06"); | |
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][mydate.getMonth()]; | |
var day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] | |
var customdate = day[mydate.getDay()] + ', ' + mydate.getDate() + ' ' + month + ' ' + mydate.getFullYear(); | |
//Custom Time Format | |
var mytime = new Date("2012-12-06 14:50:43"); | |
var hour = mytime.getHours(); | |
var mins = mytime.getMinutes(); | |
var stamp = "AM"; | |
if(hour>12){ | |
hour -= 12; | |
stamp = "PM"; | |
} | |
if(mins==0){ | |
mins = '00'; | |
} | |
else if(mins<10){ | |
mins = '0' + mins; | |
} | |
var customtime = hour + ':' + mins + ' ' + stamp; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment