So i was looking for a function to get the timespan between two dates and i've found this answer on SO
Work with a time span in Javascript
However the date is in this format 7/Nov/2012 20:30:00
and my dates are 7/11/2012 20:30:00
, and my values came from inputs
If a try to change "Nov" to "11" i got NaN on the output. Do i have to convert the dates ?
Here is the code.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
</head>
<body>
<input type="text" id ="start"value="7/Nov/2012 20:30:00" />
<input type="text" id="end" value="20/Nov/2012 19:15:00" />
</body>
</html>
The javascript
var startDate = document.getElementById("start").value;
var endDate = document.getElementById("end").value;
var date1 = new Date(startDate);
var date2 = new Date(endDate);
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);
console.log(days + " days : " + hours + " hours : " + mins + " minutes : " + seconds + " seconds");
So i was looking for a function to get the timespan between two dates and i've found this answer on SO
Work with a time span in Javascript
However the date is in this format 7/Nov/2012 20:30:00
and my dates are 7/11/2012 20:30:00
, and my values came from inputs
http://jsbin./igaruj/47/edit
If a try to change "Nov" to "11" i got NaN on the output. Do i have to convert the dates ?
Here is the code.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
</head>
<body>
<input type="text" id ="start"value="7/Nov/2012 20:30:00" />
<input type="text" id="end" value="20/Nov/2012 19:15:00" />
</body>
</html>
The javascript
var startDate = document.getElementById("start").value;
var endDate = document.getElementById("end").value;
var date1 = new Date(startDate);
var date2 = new Date(endDate);
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);
console.log(days + " days : " + hours + " hours : " + mins + " minutes : " + seconds + " seconds");
Share
Improve this question
edited May 23, 2017 at 12:29
CommunityBot
11 silver badge
asked Sep 11, 2014 at 11:21
NibblerNibbler
3341 gold badge3 silver badges15 bronze badges
3
- 1 If you don't mind using a javascript library for it; i suggest looking into momentjs momentjs. – Marvin Smit Commented Sep 11, 2014 at 11:23
- i was trying to do without any library if it's possible – Nibbler Commented Sep 11, 2014 at 11:37
- 2 Absolutely possible, but you will have to write a lot of the validation/parsing yourself, whereas a library has already done that for you. Look at an existing working library (like momentjs) and rewrite it to your heart's desire. – Marvin Smit Commented Sep 11, 2014 at 11:39
2 Answers
Reset to default 1With your 20/11/2012 20:30:00 date format:
var startDate = "7/11/2012 20:30:00";
var endDate = "20/11/2012 19:15:00";
You're getting the NaN because, it wants it in the format mm/dd/yyyy. So 20 is ofcourse an invalid month.. interchange the month and day values like:
var parts = startDate.split('/');
startDate = parts[1] + '/' + parts[0] + '/' + parts[2];
// 11/7/2012 20:30:00
var parts = endDate.split('/');
endDate = parts[1] + '/' + parts[0] + '/' + parts[2];
// 11/20/2012 9:15:00
and then do your normal time diff work and it will work:
var date1 = new Date(startDate);
var date2 = new Date(endDate);
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);
alert(days + " days : " + hours + " hours : " + mins + " minutes : " + seconds + " seconds");
//12 days 22 hours 45 minutes 0 seconds
See the DEMO here
Use JavaScript's Date
and simply call new Date("7/Nov/2012 20:30:00")
to get a JS Date object. Then you could extract the day, month and year using methods on that Date object.