Skip to content

Instantly share code, notes, and snippets.

@shivanarrthine
Last active December 9, 2015 20:49
Show Gist options
  • Save shivanarrthine/4326757 to your computer and use it in GitHub Desktop.
Save shivanarrthine/4326757 to your computer and use it in GitHub Desktop.
Custom date and time format with JavaScript
// 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