0

Friends,

I would like to calculate difference between two times. For example I have

var workedHours = '05:00:00';

var defaultWorkDay = '08:00:00';

So I'm needing to calc workedHours - defaultWorkDay and expect a '-03:00:00' result.

I've tried moment.js but with no success.

Do you have some suggestion?

quicoli
  • 612
  • 5
  • 12
  • possible duplicate of [Check time difference in Javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Cheery Nov 17 '14 at 21:38
  • 2
    moment.js should handle this beautifully. Can you post the moment.js-based code you tried? – DeeDee Nov 17 '14 at 21:38
  • 1
    If you actually tried something, you should post that code and explain how you expected it to work, and what happened instead. – Pointy Nov 17 '14 at 21:41
  • @DeeDee No it's not IMO. – Nabil Kadimi Nov 17 '14 at 21:42
  • Would you be able to convert the hours into time (like a Unix timestamp), do your arithmetic, and then convert the difference into hours? – mts7 Nov 17 '14 at 21:45
  • @Pointy I've tried. `var worked = moment.utc("2014-11-18 05:00:00","YYYY-MM-DD HH:mm:ss");` `var defaultDay = moment.utc("2014-11-18 08:00:00", "YYYY-MM-DD HH:mm:ss"); ` `var diff = moment.utc(worked).diff(moment.utc(defaultDay));` diff is "0" – quicoli Nov 18 '14 at 09:37
  • @NabilKadimi What?! Your statement makes no sense. If you're suggesting that moment.js is in fact not a good solution, I'll point you to the first accepted answer, written by a person who never used the framework yet immediately saw how to use it. – DeeDee Nov 18 '14 at 17:15

3 Answers3

1

I never used moment.js , but I think this should work for you:

    var defaultWorkDay = '08:00:00';
    var workedHours = '05:00:00';
    var a = moment.duration(defaultWorkDay);
    var b = moment.duration(workedHours);
    
    console.log( [ b.subtract(a).hours(), b.subtract(a).minutes(), b.subtract(a).seconds() ]
    ) // [-3:0:0]
<script src="http://momentjs.com/downloads/moment.min.js"></script>

http://momentjs.com/docs/#/durations/subtract/

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
0

You can do this like this if you are sure the format is always hours:minutes:secnodes...:

var start = "05:00:00";
var end = "08:00:00";

var startArr = start.split(':');
var endArr = end.split(':'); 

var diffInSeconds = 0
    + ( endArr[0] - startArr[0] ) * 3600
    + ( endArr[1] - startArr[1] ) * 60
    + ( endArr[2] - startArr[2] ) ;

console.log ( 'The difference is : ' + diffInSeconds + ' seconds.');

// => The difference is 10800 seconds.
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
0

Turn your hours into dates with time by prefixing them with a date (like below) and then do math using .getTime() on the Date object. Then convert those milliseconds into your hours, or whatever other time unit you desire.

  var date1 = new Date("2000-01-01 05:00:00");
  var date2 = new Date("2000-01-01 08:00:00");
  var msTimeOffset = date1.getTime() - date2.getTime();
  var hourTimeOffset = msTimeOffset / 1000 / 60 / 60;
  expect(hourTimeOffset).toBe(-3); // passes
Chris
  • 2,766
  • 1
  • 29
  • 34