I want to subtract the time of period 8 to 7.
I originally got this code from chat gpt but needed to rewrite everything so im trying to learn now so this may not make since due to me being a newbie
const periods = [
{ time: 9.11, name: "advisory" },
{ time: 9.57, name: "second period" },
{ time: 10.48, name: "third period" },
{ time: 11.39, name: "fourth period" },
{ time: 12.30, name: "fifth period" },
{ time: 13.05, name: "lunch" },
{ time: 13.56, name: "sixth period" },
{ time: 14.47, name: "seventh period" },
{ time: 15.40, name: "eighth period" }
];
console.log(periods[8] – periods[7])
As I stated i’m new to this and don’t know if there is a way to only get the time so i can do the math.
I tried doing periods.time but that didn’t work. I have basically 0 clue how I could do this
I read some documentation on w3schools but I still couldn’t understand. If you could help explain this would be very helpful.
2
Answers
Create functions to convert the Number to a number of minutes, and a number of minutes to a Number where the decimal represents minutes
There’s probably 100 different ways
Here’s one
As a rule of thumb – DON’T do arithmetic on relative timestamps directly. Making it within a day is relatively safe, but when you cross days, you can encounter DST (twice a year) or timezone shifts (a government decree). In medicine and finance apps that’s critical. Use date methods for date arithmetic. When the relative timestamps properly converted to dates, you can do arithmetic of those dates’ timestamps: