0

suppose I have this variable date in hour : minute : second format

var time1 = "12:34:19 PM"
var time2 = "12:29:25 PM"

How I get the differentiate (duration) from time1 and time2? And how we change it into date format to do diff process?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Rayan Suryadikara
  • 237
  • 1
  • 3
  • 17
  • 1
    And can you share your effort? – Rajesh Nov 02 '16 at 06:17
  • 1
    Possible duplicate of [Difference between two dates in minute, hours javascript](http://stackoverflow.com/questions/20839874/difference-between-two-dates-in-minute-hours-javascript) – Rajesh Nov 02 '16 at 06:18
  • I try a few methods of differentiation date,, and then I juust realize time1 data is String type, so I have to change it to date first... but any effort always end with NaN, such as using strtodate or define php variable – Rayan Suryadikara Nov 02 '16 at 06:28
  • You can use [this](http://stackoverflow.com/questions/8224459/how-to-create-a-date-object-from-string-in-javascript) as reference. Also remember, date constructor expects string to be in `MM-DD-YYYY` format. If you are open for libraries, you can look into *moment.js* – Rajesh Nov 02 '16 at 06:31
  • @Rajesh for the duplicates, I've already try the method, however as i said the problem is type data String itself... what is timeEnd and timeStart type data? – Rayan Suryadikara Nov 02 '16 at 06:32
  • No. Problem is they have datetime string and you have time string. You will have to add date part to compute a valid difference. – Rajesh Nov 02 '16 at 06:33
  • 1
    I made a [fiddle](https://jsfiddle.net/RajeshDixit/eze8q7nv/) that gives you difference in seconds without creating date object. Hope it helps – Rajesh Nov 02 '16 at 06:48

3 Answers3

1

you can convert the time new Date() just prepend a date before it and

get their timestamp using getTime() and subtract them

var duration = new Date('datetime1').getTime() - new Date('datetime2').getTime()

since the timestamp is 1000 times than the result total seconds divide it by 1000

var duration = durantion/1000;

and I just create a function that format the seconds properly to makes it looks like a valid duration time

var time1 = "2016-11-02 12:34:19 PM"
var time2 = "2016-11-02 12:29:25 PM"

time1 = new Date(time1 ).getTime();
time2 = new Date(time2 ).getTime();
var duration = (time1 - time2) / 1000;

function formatTime(seconds) {
  var minutes = Math.floor(((seconds/3600)%1)*60);
  minutes = (minutes < 10) ? '0'+minutes : minutes;
  var seconds = Math.round(((seconds/60)%1)*60);
  seconds = (seconds < 10) ? '0'+seconds : seconds;
  return minutes+':'+seconds;
}

console.log('Duration: ' + formatTime(duration)+' secs')
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

This is working script

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}
var timeStart = new Date("Mon Jan 01 2007 11:00:00 GMT+0530").getTime();
var timeEnd = new Date("Mon Jan 01 2007 11:32:51 GMT+0530").getTime();
var hourDiff = timeEnd - timeStart; //in ms
var secDiff = (hourDiff / 1000).toString(); //in s
alert(secDiff .toHHMMSS());
Shubham Sharma
  • 315
  • 2
  • 18
0

You can create a Date objects using constructor like this: Date(year, month, date, hours, minutes, seconds, ms)

in your case you can get diff:

var date = new Date(2000, 1, 1, 12, 34, 19, 0, 0),
    date2 = new Date(2000, 1, 1, 12, 39, 25, 0, 0);

console.log(date2 - date); // in ms. You can do whatever you want with this value

you can read more about Date object here.

Andrew Evt
  • 3,613
  • 1
  • 19
  • 35