I need to create a timer based on two specific dates with times provided. However, the current code I have always displays "Left time: 4h 0m 0s" as the output. It's important to note that I must use the given dates and not rely on the current date.
var endTime = '2023-09-06 19:41:55';
var startTime = '2023-09-06 15:41:55';
var targetEndTime = new Date(endTime.replace(/-/g, '/'));
var targetTime = new Date(startTime.replace(/-/g, '/'));
function updateCountdown() {
if (targetTime < targetEndTime) {
var timeDiff = targetEndTime - targetTime;
var hours = Math.floor(timeDiff / (1000 * 60 * 60));
var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
document.getElementById('countdown').innerHTML = 'Remaining time: ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
} else if (targetEndTime < targetTime) {
document.getElementById('countdown').innerHTML = 'Task completed.';
} else {
document.getElementById('countdown').innerHTML = 'Task expired.';
}
}
setInterval(updateCountdown, 1000);
updateCountdown();