Search code examples
jquerydatejs

calculate time with datejs and timejs


I am trying to calculate time with datejs and timejs but I am not getting it.

how can i do this?

var startTime ="20:55",
    endTime ="22:33",    
    st = (startTime.toString("HH:mm")),
    et=(endTime.toString("HH:mm"));
console.log(new TimeSpan(et-st));


logged: NaN:NaN:NaN { milliseconds=NaN, days=NaN, hours=NaN

Solution

  • var startTime = Date.parse("20:55"),
        endTime = Date.parse("22:33");
    
    console.log(new TimeSpan(endTime - startTime));
    

    startTime and endTime are strings, and you want them to be milliseconds.

    EDIT: I initially had .getTime() on the end of the Date.parse()s, but as geoffrey.mcgill pointed out, it wasn't necessary.