-1

I have an object and Hours is saved as a string . I need to convert the string to hours and then get the difference between the 2 variables.

const groupSchedule=[
    {"days":"sat","Hourfrom":"15:00","Hourto":"19:00"},
    {"days":"sun","Hourfrom":"15:00","Hourto":"19:00"},
    {"days":"mon","Hourfrom":"15:00","Hourto":"19:00"},
]
function numberOfHoursInWeek(groupSchedule) {
    let hours = 0;
    for (const gSchedule of groupSchedule) {
           let hour = gSchedule.Hourto.to - gSchedule.Hourfrom;
        console.log(hour);
        hours += hour;
    }
    return hours;
}

Problem in converting string to hour (NAN)

ranusharao
  • 1,826
  • 1
  • 8
  • 17
  • 1
    As far as I'm concerned, there's no such property as `to` on Hoursto in `gSchedule.Hourto.to` – kkotula Oct 22 '19 at 18:11
  • If you don't want to struggle with Date.parse without an actual date, you could consider using Moment.js. That library has better ways for parsing time strings like 'Moment("15:00", "HH:mm")'. And you can also easily run calculations on moments. – Björn Boxstart Oct 22 '19 at 19:49

2 Answers2

0

I tried to write in a very verbose way. You could do something like this:

const hoursTo = "19:33";
const hoursFrom = "14:55";
const hoursToArray = hoursTo.split(":");
const hoursFromArray = hoursFrom.split(":");
const hoursToDate = new Date(0, 0, 0, hoursToArray[0], hoursToArray[1], 0, 0); 
const hoursFromDate = new Date(0, 0, 0, hoursFromArray[0], hoursFromArray[1], 0, 0); 
const difference = Math.abs(hoursToDate - hoursFromDate) / 36e5;
console.log(hours) //4.633333333333334;
kkotula
  • 147
  • 1
  • 11
-1

The basic issue is that you are taking gSchedule.hourTo and gSchedule.hourFrom and trying to perform arithmetic with them when they are string values. You need to split the string and extract a numeric type to perform this type of mathematical calculation.

In this case the relevant numeric portion is the hours portion of the HH:MMstring, so using the split function with : as a delimiter will return a list of two string, one string of hours and one of minutes. We can then parse the hours string to get an int, float, or other numeric type.

//split time strings on the ':'
let hrToSplit = gSchedule.hourTo.split(':')
let hrFromSplit = gSchedule.hourFrom.split(':')

//parse time strings to extract hour as int
let hrToNum = parseInt(hrToSplit[0], 10)
let hrFromNum = parseInt(hrFromSplit[0], 10)

//perform whatever math is needing using the numbers themselves, not the strings
console.log(hrToNum + hrFromNum)

If you want to do some further reading on different approaches beyond the answers you got here, this is a similar question that may be useful to reference.

disc_code22
  • 109
  • 3
  • Why not just parse it as a `Date` and allow the objects do the time-related math for you? – esqew Oct 22 '19 at 18:22
  • I found this to be a simple solution since the question was only concerned with taking `HH:MM` formatted strings and performing math using the hours, but I see how parsing as a `Date` would be a valid approach as well. – disc_code22 Oct 22 '19 at 18:26