get (-) when i try too input the value for my input type="time"

error: (The specified value "08:-30" does not conform to the required format. The format is "HH:mm", "HH:mm:ss" or "HH:mm:ss.SSS" where HH is 00-23, mm is 00-59, ss is 00-59, and SSS is 000-999.)
let startTime = document.getElementById("startTime");
let endTime = document.getElementById("endTime");
let totalTime = document.getElementById("totalTime");

function CalculateTotalTime() {
//console.log(startTime.value + " " + endTime.value);
//console.log("totaltime: " + totalTime.value);

const startTimeParts = startTime.value.split(":");
const startHours = parseInt(startTimeParts[0]);
const startMinutes = parseInt(startTimeParts[1]);
console.log("Hours:" + " " + startHours + " " + "Minutes: " + startMinutes);

const endTimeParts = endTime.value.split(":");
const endHours = parseInt(endTimeParts[0]);
const endMinutes = parseInt(endTimeParts[1]);
console.log("Hours:" + " " + endHours + " " + "Minutes: " + endMinutes);

const calculatedHours = endHours - startHours;
const calculatedMinutes = endMinutes - startMinutes;
const formattedTime = `${calculatedHours.toString().padStart(2, '0')}:${calculatedMinutes.toString().padStart(2, '0')}`;
totalTime.value = formattedTime;
}

// Add event listeners to the input elements to calculate the time difference
startTime.addEventListener("input", CalculateTotalTime);
endTime.addEventListener("input", CalculateTotalTime);
totalTime.addEventListener("input", CalculateTotalTime);
let startTime = document.getElementById("startTime");
let endTime = document.getElementById("endTime");
let totalTime = document.getElementById("totalTime");

function CalculateTotalTime() {
//console.log(startTime.value + " " + endTime.value);
//console.log("totaltime: " + totalTime.value);

const startTimeParts = startTime.value.split(":");
const startHours = parseInt(startTimeParts[0]);
const startMinutes = parseInt(startTimeParts[1]);
console.log("Hours:" + " " + startHours + " " + "Minutes: " + startMinutes);

const endTimeParts = endTime.value.split(":");
const endHours = parseInt(endTimeParts[0]);
const endMinutes = parseInt(endTimeParts[1]);
console.log("Hours:" + " " + endHours + " " + "Minutes: " + endMinutes);

const calculatedHours = endHours - startHours;
const calculatedMinutes = endMinutes - startMinutes;
const formattedTime = `${calculatedHours.toString().padStart(2, '0')}:${calculatedMinutes.toString().padStart(2, '0')}`;
totalTime.value = formattedTime;
}

// Add event listeners to the input elements to calculate the time difference
startTime.addEventListener("input", CalculateTotalTime);
endTime.addEventListener("input", CalculateTotalTime);
totalTime.addEventListener("input", CalculateTotalTime);
7 Replies
Wasd | Kamikaze
Wasd | Kamikaze8mo ago
edit: i know it has to do with my calculation where if i have the end time 0 it will bugg out like that
Jochem
Jochem8mo ago
math on time is best done in unix epoch format convert start and end to epoch, subtract the two numbers, then convert back to hours and minutes from second
Wasd | Kamikaze
Wasd | Kamikaze8mo ago
i am still very new too javascript so if you would have a article or reference about it i would love to read it is epoch a function?
Jochem
Jochem8mo ago
computers usually store time in a format that's called Unix Epoch or Unix Timestamp. It's simply a very big number which represents the number of seconds since 0:00 on January 1st 1970 working with times is a bit of a nightmare in Javascript though
Wasd | Kamikaze
Wasd | Kamikaze8mo ago
hmm i see, my goal was too just have a start time and end time and get the total time for it. I thought it would be a little easier xd. You got some advice on what i could do instead or alternative way to do it? i am not trying to work with current time just with 2 times you put in yourself
Jochem
Jochem8mo ago
in this case, I'd probably convert time to seconds manually. Multiply hours by 3600, minutes by 60. Then take those two totals, subtract them, and then turn them back into hours and minutes
Wasd | Kamikaze
Wasd | Kamikaze8mo ago
ahhh good idea ill try that, thanks
Want results from more Discord servers?
Add your server
More Posts
Front-end Extentions and PrettierHello! I am wondering what are the best Visual Studio Code Extentons and settings for Front-end DevAfter one cycle of animation the screen goes white, How to avoid ithttps://codepen.io/stressunfit/pen/PoXdyNv Hello.., so i made this animation which transitions betweI'm trying to download an extension and I get this2023-10-28 14:48:14.018 [error] Error: Cannot read the extension from /c:/Users/User/.vscode/extensiQuery on JSON data type. MySQL.Excuse me.. I'm still learning on querying database. I have a column with a JSON data type like thiHow to check the screen size laptop like 14inchi want to check if this will happen when someone using 14 inch laptop.New UI in Opera and Edge messing with media queries (render window no longer full width of display)A media query of `min-width: 1920px` will now fail because the `innerWidth` in these browsers is no How to manage font size/spaces if same layout of tablet design need for PhoneI have a PSD file for tablet design. The same design when I try to develop for mobile with less thanA certain number of divs in 1 rowHello, i remember seeing a video for kevin doing smth with grid where had a stack of divs as a grid The grid(of N by N size) which I am making using flex box and wrap is getting outside of it's boxhttps://codepen.io/asel345/pen/mdvVJRO This is a segement of my code which is making a grid inside aGithub pages link not workinghow come this link doesn't work? to my website? internetslave.github.io/personal-website/ i put it o