1

I am getting datetime as inputs using multiple timepickers in AngularJS. I want to add all the time inputs(dynamic) to give me a total duration.
e.g: if i input
Date {Wed Feb 03 2016 02:07:44 GMT+0530 (India Standard Time)} Date {Wed Feb 03 2016 05:09:05 GMT+0530 (India Standard Time)}
It should return
Date {Wed Feb 03 2016 07:16:49 GMT+0530 (India Standard Time)}

Here is my code: https://jsfiddle.net/nitishhardeniya/ytndyuck/

8
  • your question and your example seems to be not in sync! In question you wanted to add two dates... but in jsfiddle code snippet you used three dates and the result is difference of first two plus the third date!! Be specific with the inputs.. Commented Feb 3, 2016 at 10:44
  • you are having only 2 dates or any number of dates can be possible? Commented Feb 3, 2016 at 11:06
  • @vkrishna that snippet works for adding a time difference to a date, whereas i am looking to add multiple datetime inputs. Here is my code: jsfiddle.net/nitishhardeniya/ytndyuck Commented Feb 3, 2016 at 11:08
  • @vkrishna I have dynamic inputs using angular that i want to iterate and show the total duration Commented Feb 3, 2016 at 11:09
  • 1
    It makes no sense to add dates as every date represents a point in time. From the example it seems that you want to add the duration elapsed from the start of the day of a date until the time of the date (2h7m44s in the example) to another date (Feb 03 2016 05:09:05 in your example) . Is this correct? Commented Feb 3, 2016 at 11:12

3 Answers 3

2

Using moment

var moment = require('moment');
var d1 = moment("Wed Feb 03 2016 02:07:44 GMT+0530", "ddd MMM DD YYYY HH:mm:ss Z");
var d2 = moment("Wed Feb 03 2016 05:09:05 GMT+0530", "ddd MMM DD YYYY HH:mm:ss Z");

var dur1 = moment.duration(d1.format("HH:mm:ss"));
var dur2 = moment.duration(d2.format("HH:mm:ss"));

var totalDur = dur1 + dur2;
var temp = d1.clone();
temp.startOf('day').add(totalDur);
console.log(temp.format()); // 2016-02-03T07:16:49+05:30
console.log(temp.format("ddd MMM DD YYYY HH:mm:ss Z")); // Wed Feb 03 2016 07:16:49 +05:30
Sign up to request clarification or add additional context in comments.

3 Comments

but what if I have dynamic inputs of dates? eg. if I have 5 inputs from different timepickers.
You can create five moment objects like d1,d2,..d5 and then calculate total duration as above. Then set one of the moment object to startOf('day') and add total duration to that. Assumptions is all dates are on same day and only time differs.
Or if you dont want to change original moment object, clone one of the moment object and then manipulate it. I have updated the code for same.
1
var difference = date2 - date1;
var newDate = new Date(date2.getTime() + difference);

1 Comment

Im using a loop to get dates from array Here is my code: jsfiddle.net/nitishhardeniya/ytndyuck
0

Try using Date.parse() method. It converts date to timestamp.

var time1 = new Date('2013-08-12 10:30');
var time2 = new Date('2013-08-12 12:30');
var time3 = new Date('2013-08-12 1:15');

var result = (Date.parse(time1) - Date.parse(time2)) + Date.parse(time3);
var resultTime = new Date(result);
alert(resultTime)

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.