Last active
August 29, 2015 14:09
-
-
Save danielcgold/f8ddef79e815a56f57f7 to your computer and use it in GitHub Desktop.
JS function to return date and time
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
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