I'm trying to display the current time and date on my website but having issues because I need it displayed a certain way and with an hour added ahead.
For the first one I need it displayed like this:
20 Sep 2022 *current time* (12-hour format with am and pm)
second one:
20 Sep 2022 *current time +1hr* (12-hour format with am and pm)
I tried this code of Codepen but I need the time displayed after the date and it also shows as a short date on my site for some reason.
function DateAndTime() {
var dt = new Date();
var Hours = dt.getHours();
var Min = dt.getMinutes();
var Sec = dt.getSeconds();
var days = [
"",
"",
"",
"",
"",
"",
""
];
//strings
var months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"June",
"July",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
if (Min < 10) {
Min === "0" + Min;
} //displays two digits even Min less than 10
if (Sec < 10) {
Sec === "0" + Sec;
} //displays two digits even Sec less than 10
var suffix = "AM"; //cunverting 24Hours to 12Hours with AM & PM suffix
if (Hours >= 12) {
suffix = "PM";
Hours = Hours - 12;
}
if (Hours === 0) {
Hours = 12;
}
document.getElementById("time").innerHTML =
Hours + "Hours : " + Min + "Min : " + Sec + "Sec " + suffix + ".";
document.getElementById("date").innerHTML =
days[dt.getDay()] +
", " +
dt.getDate() +
" " +
months[dt.getMonth()] +
" " +
dt.getFullYear();
}
new DateAndTime();
setInterval("DateAndTime()", 1000);
the above code shows like this:
7Hours : 9Min : 47Sec PM.
, 20 Sep 2022
I need it displayed like this
20 Sep 2022 7:10 pm
20 Sep 2022 8:10 pm (one hour added to current time)
I'm pretty new to coding so sorry if it's a simple question trying to edit codes of Codepen seems to mess one thing or another up for me always.
thanks.