0

I have start date time and end time,i need to split how many days , hours ,minutes in the two dates

for example , startdatetime = "09-06-2017 10:30" enddatetime = "10-06-2017 11:45"

i need this result : 1 day 1 hour and 15 minutes

I try this one

var t = end - start;
var z = parseInt(t / 1000 / 60);
var time = display(z);
function display(a)
{
    console.log(a);
    var hours = Math.trunc(a/60);
    var minutes = a % 60; 
    var one_day=1000*60*60*24
    var days = Math.ceil(a/one_day)
    var time = [hours,minutes,days];
    return time;
}

i get the following 1day 24 hours and 15 minutes , can anyone help me , if its new logic means i will change into it,thanks in advance

Elamparithi.P
  • 149
  • 3
  • 18

5 Answers5

1

Well, if you look at documentation for javascript Date objects, there is a getTime() method . You can also use the valueOf() method. They both return the number of milliseconds representing your Date object.

You can simply call that on both Date objects and then find the difference. Once you have the difference you can find the amount of secs, mins , hrs, days, etc. Here is an example:

var start = new Date(*some date*);
var end = new Date(*some date*);
var dif = end.valueOf() - start.valueOf();
if (dif >= 0) {
    var secs = Math.floor(dif / 1000 % 60);
    var mins = Math.floor(dif / 1000 / 60 % 60);
    var hrs = Math.floor(dif / 1000 / 60 / 60 % 24);
    var days =
        Math.floor(dif / 1000 / 60 / 60 / 24 % 365);
    var yrs =
        Math.floor(dif / 1000 / 60 / 60 / 24 / 365);
MagicGuy52
  • 571
  • 1
  • 6
  • 11
1

Try the following:

var t = end - start;
var z = parseInt(t / 1000 / 60);
var time = display(z);
function display(minutes)
{
    var hours = (minutes / 60 | 0) % 24;
    var minutes = (minutes | 0) % 60;
    var days = minutes / 60 / 24 | 0;
    return [hours, minutes, days];
}

Note that in javascript, doing x | 0 is the same as Math.floor(x).

1

It looks to me like your calculation for hours still has the days in it. Once you have established the days, just subtract those out when you calculate the hours.

var start = new Date("June 09, 2017 10:30:00");
var end = new Date("June 10, 2017 11:45:00");
var t = end - start;
var z = parseInt(t / 1000 / 60);
var time = display(z);
console.log(time);
function display(a)
{
    var minutes = a % 60; 
    var one_day=1000*60*60*24
    var days = Math.ceil(a/one_day)
    var hours = Math.trunc((a-(days*1440))/60);
    var time = [hours,minutes,days];
    return time;
}

Having said that, I highly recommend moment.js to handle this type of thing, if you can.

aldenh
  • 11
  • 2
  • hi , i am using moment js only , i convert the time into time stamp , start time is "1496948401000" and endtime is 1497132000000 – Elamparithi.P Jun 08 '17 at 18:26
1

Using momentjs, you can :

Here a live sample:

var startdatetime = "2017-06-09T07:00:01.000Z";
var enddatetime = "2017-06-10T09:00:00.000Z";
// Parse input
var mStart = moment.utc(startdatetime);
var mEnd = moment.utc(enddatetime);
// Calculate difference and create duration
var dur = moment.duration( mEnd.diff(mStart) );
// Show the result
console.log(dur.days() + ' days ' + dur.hours() + ' hour ' + dur.minutes() + ' minutes');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

If you want you can use moment-duration-format plug-in to get the same result using format() method on duration. Here a working sample:

var startdatetime = "2017-06-09T07:00:01.000Z";
var enddatetime = "2017-06-10T09:00:00.000Z";
// Parse input
var mStart = moment.utc(startdatetime);
var mEnd = moment.utc(enddatetime);
// Calculate difference and create duration
var dur = moment.duration( mEnd.diff(mStart) );
// Show the result
console.log(dur.format('d [day] h [hour] m [minutes]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
  • i am using moment js in nodejs is it working in node?i have utc time format ,pls help me to fix – Elamparithi.P Jun 08 '17 at 18:41
  • @Elamparithi.P yes the provided solution will work also on node. If your input is UTC parse it using [`moment.utc`](http://momentjs.com/docs/#/parsing/utc/) – VincenzoC Jun 08 '17 at 21:11
  • `start: '2017-06-09T07:00:01.000Z' ` `end: '2017-06-10T09:00:00.000Z' ` ,this is my input its an utc timestamp – Elamparithi.P Jun 09 '17 at 06:45
  • 1
    @Elamparithi.P updated my answer after you comment, simply use [`moment.utc`](http://momentjs.com/docs/#/parsing/utc/) as stated in previous comment. – VincenzoC Jun 09 '17 at 07:33
  • hi @VinocenzoC ,i am getting wrong output , can u pls help to figure out ,i am giving the below start and end date , end : '2017-07-10T14:00:00.000Z',start : '2017-06-10T13:30:00.000Z' var mStart = moment(req.body.start),var mEnd = moment(req.body.end) // Calculate difference and create duration var dur = moment.duration( mEnd.diff(mStart)) console.log(dur.months() +' months '+dur.weeks()+' weeks '+dur.days() + ' days ' + dur.hours() + ' hour ' + dur.minutes() + ' minutes') i got this result 0months 4 weeks 30 days 0 hour 30 minutes but i need 0 months 4 weeks 0 days 0 hour 30 minutes – Elamparithi.P Jun 10 '17 at 13:49
  • 1
    @Elamparithi.P you are right, the problem is that difference between your `start` and `end` is 30 days, so `weeks()` ouputs 4 and and `days()` outputs 30. The moment-duration-format solution gives a better ouput (`4 weeks 2 days` since it calculates 7 days per week), even if it isn't exactly what you are expecting. I suggest to ask another question showing this new requirement (don't forget to add code sample), because this situation differs from your original question (and I fear it is more complicated to handle) – VincenzoC Jun 11 '17 at 10:08
1
var startDateTime = 1497029400000;
var endDateTime = 1497120300000;
var timeDifference = endDateTime - startDateTime
// with the given dates, days equals 1.0520833333333333
// we want to extract the trailing decimal values using modulus to get the other times

function getTimeDifference(timeDifference) {
  var days = timeDifference/1000/60/60/24
  days >= 1 
    ? var dayCount = Math.trunc(days); // store the day count
    : var dayCount = 0; // it is less than one day

  // get the remaining hours
  var hours = (days % 1) * 24;
  var hoursCount = Math.trunc((days % 1) * 24);

  // get the remaining minutes
  var minutesCount = Math.ceil((hours % 1) * 60);
}