0
votes

I'm writing custom function for google sheets. One part should calculate delta of two difference times.

  function timeOut(input) {
    var difference = input[0][1]-input[0][0];
    var output = new Date(difference);

    return output;
  }

Variable difference contains zero. The problem is that new Date(0) is returning

Thu Jan 01 1970 01:00:00 GMT+0100 (CET)

which instead of

Thu Jan 01 1970 00:00:00 GMT+0100 (CET)

So difference of 9 hours minus 9 hours is 1 hours, which is obviously wrong.

Could you pinpoint me, what I'm doing wrong ?

2

2 Answers

1
votes

00:00:00 GMT+0100 would be -1 hour since it is +0100, the timezone matters, 01:00:00 GMT+0100 really is 0 hours.

If you want to get GMT, you can use toGMTString():

var date = new Date(0);
alert(date.toGMTString()); // prints Thu, 01 Jan 1970 00:00:00 GMT
0
votes

It's actually obviously correct, on the 00:00:00 of the GMT time, in +0100 the time was 01:00:00.

If you need the GMT time you can:

var GMTTime = new Date( -new Date().getTimezoneOffset() * 60000 );