Skip to content

Instantly share code, notes, and snippets.

@anxp
Created March 17, 2020 08:22
Show Gist options
  • Save anxp/69b3f5397cf98dc812daa12fc0472f3a to your computer and use it in GitHub Desktop.
Save anxp/69b3f5397cf98dc812daa12fc0472f3a to your computer and use it in GitHub Desktop.
function getCurrentDateTime () {
var objToday = new Date();
var domEnder = '';
if (/1/.test(String(parseInt((objToday + "").charAt(0), 10)))) {
domEnder = 'th';
} else if (parseInt((objToday + "").charAt(1), 10) === 1) {
domEnder = 'st';
} else if (parseInt((objToday + "").charAt(1), 10) === 2) {
domEnder = 'nd';
} else if (parseInt((objToday + "").charAt(1), 10) === 3) {
domEnder = 'rd';
} else {
domEnder = 'th';
}
var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dayOfWeek = weekday[objToday.getDay()];
var dayOfMonth = (objToday.getDate() < 10) ? '0' + objToday.getDate() + domEnder : objToday.getDate() + domEnder;
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var curMonth = months[objToday.getMonth()];
var curYear = objToday.getFullYear();
var curHour = objToday.getHours() > 12 ? objToday.getHours() - 12 : (objToday.getHours() < 10 ? '0' + objToday.getHours() : objToday.getHours());
var curMinute = objToday.getMinutes() < 10 ? '0' + objToday.getMinutes() : objToday.getMinutes();
var curSeconds = objToday.getSeconds() < 10 ? '0' + objToday.getSeconds() : objToday.getSeconds();
var curMeridiem = objToday.getHours() > 12 ? 'PM' : 'AM';
return curHour + ':' + curMinute + '.' + curSeconds + ' ' + curMeridiem + '; ' + dayOfWeek + ', ' + dayOfMonth + ' of ' + curMonth + ', ' + curYear;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment