2

I have two text-input-fields where users can enter a time in format "hh:mm". The main part looks like the following code:

    var time1 = new Date("01/01/2013 " + time1input);   
    var time2 = new Date("01/01/2013 " + time2input);
    var difference = new Date(time2 - time1);

The result is always 01:00 to high (e.g. 08:00 - 04:00 results in 05:00 which is obviously wrong :) ). Did i misunderstood something?

juleee
  • 456
  • 1
  • 7
  • 20

4 Answers4

5

You can't work with dates like that.

Try the following instead:

var time1input = "04:00";
var time2input = "08:00";

var time1 = new Date("01/01/2013 " + time1input);   
var time2 = new Date("01/01/2013 " + time2input);
var difference = time2.getTime() - time1.getTime();
console.log(difference / 60 / 60 / 1000); //<- whittle this down to hours

That will log out '4'.

If you're working with times, use something like http://momentjs.com/, which lets you do cool stuff like..

difference = moment(time2).diff(time1, 'hours'); //<- will return 4

Format (hh:mm): (assuming use of difference from first example)

var hours = Math.floor(difference / 60 / 60 / 1000);
var minutes = Math.round(difference / 60 / 1000) % 60;
var formattedString = (hours > 10 ? hours : "0" + hours) + ":" + (minutes > 10 ? minutes : "0" + minutes);

A 4 hour difference will return "04:00", a 13 hour, 30 minute difference will return 13:30.

Stephen
  • 5,362
  • 1
  • 22
  • 33
  • 1
    I was writing about Moment.js :( – gustavohenke Jul 01 '13 at 19:45
  • 1
    Aw, sorry man. Super useful library, always my 'goto' whenever anyone says "Date". – Stephen Jul 01 '13 at 19:46
  • wow cool. but what if i want the difference to be in the format hh:mm? – juleee Jul 01 '13 at 19:48
  • Updated. There are a hundred different ways of doing it really.. and the example I gave doesn't take into account multi-day differences (i.e. 2 days will give 48 hours). You'll kind of have to hack away at that. – Stephen Jul 01 '13 at 19:54
  • var time1 = new Date("01/01/2013 " + departureTimeInput); says invalid date. although departureTimeInput is "04:00" – juleee Jul 01 '13 at 20:12
0

Look at the getTime method. This might work:

var difference = new Date(time2.getTime() - time1.getTime());
Scott Isaacs
  • 1,168
  • 7
  • 16
0

Usually when we calculate time differences.

we used to convert it to milliseconds, find difference and convert back to a format.

check this post

Praveen
  • 55,303
  • 33
  • 133
  • 164
0

also:

var time1 = new Date("01/01/2013 " + time1input)
var time2 = new Date("01/01/2013 " + time2input)
var difference = time2 - time1

difference is a difference in milliseconds, so to get a difference in hours you can do:

var time1 = new Date("01/01/2013 04:00")
var time2 = new Date("01/01/2013 08:00")
var difference = (time2 - time1)/1000/60/60
console.log(difference)
# => 4

Notice that you can operate with Date objects directly, it's not necessary to call .getTime()

p.s. a similar question has already been answered a long long time ago

Community
  • 1
  • 1
trushkevich
  • 2,657
  • 1
  • 28
  • 37