Skip to content

Instantly share code, notes, and snippets.

@bls1999
Created September 30, 2019 06:21
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 bls1999/11694d8c0b43e7e4d6edfe46d140496f to your computer and use it in GitHub Desktop.
Save bls1999/11694d8c0b43e7e4d6edfe46d140496f to your computer and use it in GitHub Desktop.
Formatted JavaScript current date/time function.
// outputs as: Monday, September 30, 2019 at 2:21:45am
function getReadableDate()
{
// init arrays
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// get date var to work with
var now = new Date();
// convert to 12-hour time
var currentHour = now.getHours();
var meridiem = 'am';
if (currentHour == 0) {
currentHour = 12; // if 0:00, it's 12am
} else if (currentHour >= 12) {
meridiem = 'pm'; // if 12:00 or later, it's pm
if (currentHour >= 13 && currentHour <= 23) {
currentHour = currentHour - 12; // e.g. if 13:00, it's 1pm
}
}
// make sure minutes/seconds aren't omitting leading zeros
var currentMinute = now.getMinutes();
currentMinute = currentMinute < 10 ? currentMinute = '0' + currentMinute : currentMinute;
var currentSecond = now.getSeconds();
currentSecond = currentSecond < 10 ? currentSecond = '0' + currentSecond : currentSecond;
// return readable date
return days[now.getDay()] + ', ' + months[now.getMonth()] + ' ' + now.getDate() + ', ' + now.getFullYear() + ' at ' + currentHour + ':' + currentMinute + ':' + currentSecond + meridiem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment