skip to Main Content

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


  1. 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

    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" }
    ];
    
    const timeToMinutes = (time) => time * 100 - Math.floor(time) * 40;
    const minutesToTime = (minutes) => (minutes - minutes % 60) / 60 + (minutes % 60) / 100;
    const answerInMinutes = timeToMinutes(periods[8].time) - timeToMinutes(periods[7].time)
    console.log(minutesToTime(answerInMinutes))
    Login or Signup to reply.
  2. 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:

    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" }
    ];
    
    const str2time = (time, dt = new Date, [h, m] = time.toString().split('.').map(e => e.padEnd(2, '0'))) => 
      new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(), h, m - dt.getTimezoneOffset());
    
    const result = str2time(periods[8].time) - str2time(periods[7].time);
    
    console.log(`${result % 60000}.${result / 60000 | 0}`);
    
    // you can format with Intl.DateTimeFormat also:
    const time = new Date;
    time.setHours(0, 0, 0, 0);
    time.setMilliseconds(result);
    console.log(time.toLocaleTimeString('en', {hourCycle: 'h23', minutes: '2digit', hours: '2digit'}));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search