0

I have two times, say timeS = '00.00.00' and timeE = '10.00.00'

I want to get the time difference between the two times. One way is the following manner,

//create date format          
var timeStart = new Date("01/01/2007 " + timeS).getHours();
var timeEnd = new Date("01/01/2007 " + timeE).getHours();

var hourDiff = timeEnd - timeStart;

Here, I had to get the desired result using the Date format. Is there any other way to achieve this, without explicitly making a Date object?

Note: Instead of marking it a duplicate, do observe that I am asking for some other alternative ways.

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • Why is making Data object so bad for you? – NoOorZ24 Mar 20 '18 at 06:38
  • @NoOorZ24, No its not bad for me. All I am interested in is to find some alternative solutions. You can call it a touch of "curiosity" – Saswat Mar 20 '18 at 06:38
  • `'10:00:00'.split(':').reduce((sum, curr) => sum * 60 + +curr, 0)` – Roland Starke Mar 20 '18 at 06:45
  • Do not use the built-in parser, particularly for non–compliant strings. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Mar 20 '18 at 07:18

2 Answers2

2

Convert the hh.mm.ss format into equivalent seconds by parsing.

var timeS = '00.00.00';
var timeE = '10.00.00';

function convertToSeconds(timeString) {
    var timeSplit = timeString.split('.');
    return timeSplit.reduce((result, item, index) => {
        return result + Number(item) * Math.pow(60, timeSplit.length - index - 1)
    }, 0);
}

const secondDifference = convertToSeconds(timeE) - convertToSeconds(timeS);

console.log(secondDifference);
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • The arrow function can be reduced to `return timeSplit.reduce((result, item, index) => result + item * Math.pow(60, timeSplit.length - index), 0)`. ;-) – RobG Mar 20 '18 at 07:25
  • @Saswat just fixed a bug in this, if you copied this for use, please note. – Ayush Gupta Mar 20 '18 at 09:23
1

You need to convert this time to a Date first

var convertToMS = time => { 
   var d = new Date(1,1,1);
   d.setHours.apply( d, time.split(".").map( Number ) );
   return d.getTime();
};

var diffOutput = convertToMS( timeE ) - convertToMS( timeS );

Demo

var convertToMS = time => {
  var d = new Date();
  d.setHours.apply(d, time.split(".").map(Number));
  return d.getTime();
};

var timeS = '00.00.00';
var timeE = '10.00.00';

var diffOutput = convertToMS(timeE) - convertToMS(timeS);
console.log(diffOutput)

Edit

As suggested by @RobG, check this alternate solution using UTC

var convertToMS = time => Date.UTC(1,1,1, ...time.split('.'));
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • This is prone to rare bugs due to daylight saving affecting the time value. A Date object should really only be used where dates are involved. – RobG Mar 20 '18 at 07:21
  • @RobG How is the above code prone to bugs due to daylight savings. Please share. – gurvinder372 Mar 20 '18 at 08:59
  • You're setting the local time. If the first time is before the daylight saving change and the second time is after, there will be a discrepancy equal to the daylight saving offset, either minus or plus, depending on whether going into or out of daylight saving respectively. – RobG Mar 20 '18 at 11:11
  • @RobG In that case, if I change it to use `new Date(1,1,1)`, then all invocations will use the same data and given time only, right? – gurvinder372 Mar 20 '18 at 11:20
  • Yes (noting that creates a date for 1901-02-01) and assuming it doesn't coincide with a daylight saving boundary anywhere (which seems likely), or use UTC, so the function becomes `var convertToMS = time => Date.UTC(1,1,1, ...time.split('.'))`. – RobG Mar 20 '18 at 11:27