0

I have two strings like

var x = "22:10";
var y = "23:15";

How can i calculate using javascript the time is remaining to x get into y?

in this case would be

var final = "01:05"

Thanks!

Weslley Araujo
  • 668
  • 1
  • 6
  • 18
  • 2
    What have you tried so far? What I would do is convert them both to seconds -> calculate the difference -> converting the difference to hours. – Jeroen Jan 05 '14 at 00:20
  • Have a look at [this question](http://stackoverflow.com/questions/4944750/how-to-subtract-date-time-in-javascript). – eebbesen Jan 05 '14 at 00:21
  • @JeroenJK I have another method :) – Cilan Jan 05 '14 at 00:25
  • Do you need to allow for times that span midnight? (E.g., if `y` was `"03:15"`?) – nnnnnn Jan 05 '14 at 00:25
  • I may found real good solution at this question that @eebbesen posted here [link](http://jsfiddle.net/zANNt/1/) – Weslley Araujo Jan 05 '14 at 00:25
  • Yes @nnnnnn i should care about times that span midnight :P – Weslley Araujo Jan 05 '14 at 00:26
  • The method I posted before, but first checking if `var y` is smaller than `var x`. If that's the case then you just take the time of `var x` (in seconds) to 24:00 in seconds + the time of `var y` (in seconds). EDIT:: Oh wait, you could also just use minutes instead of seconds! :D – Jeroen Jan 05 '14 at 00:32
  • Would it ever need to be more than 24 hours (e.g. three days away)? – Rhyono Jan 05 '14 at 00:35
  • Are you going to accept an answer..? – Cilan Feb 02 '14 at 14:12

3 Answers3

4

@Man of Snow solution is simple and I think it works well, but if you will need more complex example for working with dates you can review my code:

var x = "22:10";
var y = "23:15";

function createDate(v) {
    var date = new Date(),
        va = v.split(':');

    date.setHours(va[0]);
    date.setMinutes(va[1]);

    return date;
}

function getDiff(x, y) {
    var date1 = createDate(x),
        date2 = createDate(y);

    var result = new Date();
    result.setTime(date2.getTime() - date1.getTime());

    var hours = result.getUTCHours().toString(),
        minutes = result.getUTCMinutes().toString();

    if (hours.length == 1) {
        hours = '0' + hours;
    }

    if (minutes.length == 1) {
        minutes = '0' + minutes;
    }

    return hours + ':' + minutes;
}

var diff = getDiff(x, y);

alert(diff);

JSFIDDLE

Filip Górny
  • 1,749
  • 16
  • 24
1

Where's the code?

Here's the code!

var x = "22:10";
var y = "23:15";

var xTotal = parseInt(x.split(':')[0] * 60) + parseInt(x.split(':')[1]);
var yTotal = parseInt(y.split(':')[0] * 60) + parseInt(y.split(':')[1]);
var total = yTotal - xTotal;

var minutes = Math.floor(total / 60);
var seconds = total - minutes * 60;

if(seconds.toString().length == 1)
{
    seconds = seconds.toString();
    seconds = '0' + seconds;
}

var final = minutes.toString() + ':' + seconds.toString();
console.log(final);

which logs 1:05 (as you can see will also work with minutes).

How's it work?

I'm glad you asked, Ben. Well, it converts the first and second part around the colon to seconds (minutes * 40), of course parseInt, because they're Strings! Anyways, it now divides that number's rounded state (Math.floor() for you programmers at home) by 60 to get the amount of minutes. And you know what it does then, Ben?

(Ben: Nope, but I wish it did what I thought.)

I'm glad you say so, Ben! Well, here's what it does then: It actually takes that total number of seconds and subtracts it by the number of minutes * 60, and what does it do now? Well, I'll tell you, Ben! It actually converts it into a string! Minutes, a colon, then seconds! How cool's that?

(Ben: Great, we'll be in touch. Next?)

I'm too lazy to Copy/Paste this. Where's the Fiddle?

It's right over here, Ben! (which alerts instead of logs)

...who's Ben???

Cilan
  • 13,101
  • 3
  • 34
  • 51
  • I didn't downvote, but doesn't this output `"1:5"`? Also, remember that `y` could be after midnight... – nnnnnn Jan 05 '14 at 00:27
  • @nnnnnn Edited, hope it gets an upvote now to pay for the downvote – Cilan Jan 05 '14 at 00:31
  • `final` isn't a reserved word in ES5 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words#In_ECMAScript_5) – Oriol Jan 05 '14 at 00:42
  • The fiddle is alerting "45:25" when the answer _should_ be "1:05". – nnnnnn Jan 05 '14 at 01:34
  • @nnnnnn Yeah, I have a problem with rushing through questions. I've edited my answer so it now displays '1:05'. – Cilan Jan 05 '14 at 03:45
1

You can use

function padding(str, len, char) {
    return (Array(len).join(char)+str).substr(-len);
}

var x = "22:10",
    y = "23:15",
    day = "Sun Jan 05 2014 ", // or whatever
    final = new Date(new Date(day+y)-new Date(day+x));
final = padding(final.getUTCHours(), 2, 0) + ':' + padding(final.getUTCMinutes(), 2, 0);
Oriol
  • 274,082
  • 63
  • 437
  • 513