I am trying to make a countdown timer for ticket response expiry. My code will successfully calculate the time left when dealing with hours bar the extra hour (+0100) GMT.
I have tried dealing with this in all manner of ways suggested on here to no avail. Any suggestions? Should I give in & learn Luxon?
The converttoUTC function seen is not called as it has not worked & only messed up the calculation further.
The dates that's been pulled from the table is in the following format 2022-04-15 17:47:19 The Time Limits being pulled from the table are in the following format, "15 mins" "6 hours".
<!--===========================================================================================-->
<script>
function func(creationDatePlusLimit) {
// var dateValue= document.getElementById("date").value;
var date = Math.abs((new Date().getTime() / 1000).toFixed(0));
var date2 = Math.abs((new Date(createdOnDate).getTime() / 1000).toFixed(0));
var diff = date2 - date;
var days = Math.floor(diff / 86400);
var hours = Math.floor(diff / 3600) % 24;
var mins = Math.floor(diff / 60) % 60;
var secs = diff % 60;
// document.getElementById("data").innerHTML = days + " days, " + hours + ":" + mins + ":" + secs;
if (days>=0) {
return days + " days, " + hours + ":" + mins + ":" + secs;
} else {
return "late";
}
}
const loopThroughTableRows = () => {
const tableRows = Array.from(document.getElementsByTagName('tr'));
tableRows.shift(); // removes first one, header
tableRows.forEach(row => {
var rowCols = row.getElementsByTagName('td');
var createdOnDate = rowCols[3];
var timeLimit = rowCols[7];
function convertDateToUTC(date) {
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
}
const createdDate = new Date(createdOnDate.innerText);
// if time limit is in days, remove text, & add to creation date-------------------//
var limitdays = timeLimit.innerText;
if (limitdays.includes(" days")) {
limitdays = limitdays.replace("days", "");
limitdays= parseInt(limitdays);
function addDaysToDate(createddate, days) {
var result = createddate;
result.setDate(createddate.getDate()+days);
return result;
}
var newDate = addDaysToDate(createdDate, limitdays);
// format newdate to iso & remove unwanted characters
newDate = newDate.toISOString();
if (newDate.includes("T")) {
newDate = newDate.replace("T", " ");
}
if (newDate.includes(".000Z")) {
newDate = newDate.replace(".000Z", "");
}
};
//===================================================================================//
// if time limit is in hours, remove text, & add to creation date-------------------//
// var limithours = timeLimit.innerText;
// if (limithours.includes(" hours")) {
// limithours = limithours.replace("hours", "");
// limithours= parseInt(limithours);
//
// function addHoursToDate(createddate, hours) {
// var result = createddate;
// // result.setHours(createddate.getDate()+6);
// return result;
// }
// var newDate = addHoursToDate(createdDate, limithours);
//
// // format newdate to iso & remove unwanted characters
// newDate = newDate.toISOString();
// if (newDate.includes("T")) {
// newDate = newDate.replace("T", " ");
// }
// if (newDate.includes(".000Z")) {
// newDate = newDate.replace(".000Z", "");
// }
// };
//===================================================================================//
const testRow = rowCols[8];
const timeDifference = func(newDate);
testRow.innerText = newDate;
});
}
loopThroughTableRows();
setInterval(loopThroughTableRows, 1000)
</script>