Skip to content

Instantly share code, notes, and snippets.

@danielcgold
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcgold/f8ddef79e815a56f57f7 to your computer and use it in GitHub Desktop.
Save danielcgold/f8ddef79e815a56f57f7 to your computer and use it in GitHub Desktop.
JS function to return date and time
function getDateAndTime(){
// get the date string from JS
var d = new Date();
var currDate = d.getDate();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
var currHour = d.getHours();
var currMin = d.getMinutes();
var currSec = d.getSeconds();
var amOrPm = "";
// Months are 0 based, increment by 1 to get the real human month
currMonth += 1
// Figure out AM vs PM based on the current hour
if (currHour < 12){
amOrPm = "AM";
} else {
amOrPm = "PM";
}
// Get non 24 hours based hour time
if (currHour == 0){
currHour = 12;
}
if (currHour > 12){
currHour = currHour - 12;
}
// Get current minute with a leading 0
currMin = currMin + "";
if (currMin.length == 1){
currMin = "0" + currMin;
}
// Format the date
return currMonth + "/" + currDate + "/" + currYear + " at " + currHour + ":" + currMin + ":" + currSec + " " + amOrPm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment