function displayNiceTime(date){
// getHours returns the hours in local time zone from 0 to 23
var hours = date.getHours()
// getMinutes returns the minutes in local time zone from 0 to 59
var minutes = date.getMinutes()
var meridiem = " AM"
// convert to 12-hour time format
if (hours > 12) {
hours = hours - 12
meridiem = ' PM'
}
else if (hours === 0){
hours = 12
}
// minutes should always be two digits long
if (minutes < 10) {
minutes = "0" + minutes.toString()
}
return hours + ':' + minutes + meridiem
}
function getTimeStr() {
var dt = new Date();
var d = dt.toLocaleDateString();
var t = dt.toLocaleTimeString();
t = t.replace(/\u200E/g, '');
t = t.replace(/^([^\d]*\d{1,2}:\d{1,2}):\d{1,2}([^\d]*)$/, '$1$2');
var result = d + ' ' + t;
return result;
}
var myVar=setInterval(function(){myTimer()},1000);
function myTimer() {
var d = new Date();
currentNow = d.toLocaleTimeString();
nowArray = currentNow.split(':');
filteredNow = nowArray[0]+':'+nowArray[1];
document.getElementById("demo").innerHTML = filteredNow;
}
let d = new Date(2022, 2, 21, 7, 45, 0);
let s = d.toLocaleString([], { dateStyle: 'short', timeStyle: 'short' });
console.log(s);
// '2/21/22, 7:45 AM'