It's very often we come across requirement when we need to calculate time difference between two dates in Javascript. Here is a simple Javascript function which can do it in quick and dirty way
Dirty because I would have to say because I would have made it little more generalized by provide options and formats which convert string to date automatically..!
Anyway for that matter I would rather suggest using php.js which is interesting and helpful library which let's us use lot of PHP functions in Javascript.. It includes hundreds of useful and handy functions which we are used to while we develop PHP Applications.
This simple functions takes only 2 arguments as Javascript Date objects and in return provides an object with days, hours, minutes and seconds as the property in difference between these 2 date provided as parameter to function.
Here is the code for simple Date-time difference calculation:
// Simple function to calculate time difference between 2 Javascript date objects function get_time_difference(earlierDate,laterDate) { var nTotalDiff = laterDate.getTime() - earlierDate.getTime(); var oDiff = new Object(); oDiff.days = Math.floor(nTotalDiff/1000/60/60/24); nTotalDiff -= oDiff.days*1000*60*60*24; oDiff.hours = Math.floor(nTotalDiff/1000/60/60); nTotalDiff -= oDiff.hours*1000*60*60; oDiff.minutes = Math.floor(nTotalDiff/1000/60); nTotalDiff -= oDiff.minutes*1000*60; oDiff.seconds = Math.floor(nTotalDiff/1000); return oDiff; } // Function Usage function use_time_difference() { dateWhenIndiaWonFirstCricketWorldCup = new Date(1983, 6, 25, 5, 0, 0); dateCurrent = new Date(); oDiff = get_time_difference(dateWhenIndiaWonFirstCricketWorldCup, dateCurrent); alert("It has been " + oDiff.days + " days since India won it's first cricket worldcup"); } use_time_difference();
Go see live demonstration or download and view source code.
I hope it would help some Javascript beginners.
Pingback: Tweets that mention Calculate Date/Time difference - Simple Javascript code snippet | BLOGS@DiGiTSS -- Topsy.com
#1 by deepika on March 7, 2011 - 4:51 pm
Quote
I need javascript code for calculating the number of days between two dates
#2 by Dharmavirsinh Jhala on March 7, 2011 - 11:52 pm
Quote
Hi Deepika,
Above code does the same thing, you can see demo live in action @ following link: http://www.digitss.com/code_snippets/javascript_t…
This link was broken down but I have fixed that now. Basically this function give you all 3 difference Days, Hours and Minutes; out of it what you want to use is up to you.
#3 by Guest on April 13, 2011 - 7:07 pm
Quote
chage the date of world cup
#4 by Dharmavirsinh Jhala on April 13, 2011 - 9:39 pm
Quote
That`s India`s first Cricket Wordcup dude and it will stay first forever.!
#5 by elbrusnt on May 23, 2011 - 1:03 pm
Quote
function get_time_difference(earlierDate,laterDate)
{
var nTotalDiff = laterDate.getTime() – earlierDate.getTime();
var one_day=1000*60*60*24;
all_days = Math.floor(nTotalDiff/bir_gun);
return all_days;
}
// Function Usage
function use_time_difference()
{
dateWhenIndiaWonFirstCricketWorldCup = new Date(2011, 05, 23, 02, 55, 0);
dateCurrent = new Date();
oDiff = get_time_difference(dateWhenIndiaWonFirstCricketWorldCup, dateCurrent);
alert("It has been " + oDiff + " days since India won it's first cricket worldcup");
}
use_time_difference();
#6 by andrew on July 3, 2011 - 5:09 am
Quote
como puedo hacer que esa funcion tome solo las horas sin los los fines de semana, y con horas laborales de 8 – 18 hours
how to do that same function take only hours without the weekends and with bussines hours of 8 -18 h
#7 by Dharmavirsinh Jhala on July 4, 2011 - 11:42 pm
Quote
Hi Andrew,
This is good question, taking only 8-18 hours will be simple but to take only business days and deduct saturdays and sundays; we need to write some custom logic..
#8 by dhanesh mane on July 11, 2011 - 10:42 am
Quote
its not checking difference properly when dates r same. oDiff.minutes should give proper minutes difference.
Try current date :
dateCurrent = new Date(2011, 07, 01, 23, 00, 0);
dateWhenIndiaWonFirstCricketWorldCup = new Date(2011, 07, 01, 22, 00, 0);
#9 by Dharmavirsinh Jhala on July 23, 2011 - 3:34 pm
Quote
o oow……. It means I will have to take a look at it.
Dhanesh, Thanks for stopping by and letting me know this.
#10 by taniya on November 23, 2011 - 12:44 pm
Quote
var selectedDate = txt.value + "/" + "00" + "/" + "00";
var arrdate=selectedDate.split("/");
var actualdatetobechek = new Date(arrdate[2], arrdate[1] – 1 , arrdate[0] ,arrdate[3],arrdate[4]);
var nowDatetime = new Date();
var _Diff;
_Diff=Math.ceil(nowDatetime.getTime()-actualdatetobechek.getTime());
if (_Diff < 0 )
{
alert('Select today or a date less than today!');
txt.value = '';
}
#11 by Altaf on March 12, 2012 - 4:53 am
Quote
How about the years, months, days, minutes, sec and millisecs? I need Years and months in the calculation
#12 by Dharmavirsinh Jhala on March 20, 2012 - 11:27 pm
Quote
Hay Altaf,
That's pretty simple yaar, you have days now you can do whatever you want.
Divide them by 30 to get the numbers, divide them by 365 to get the year and so on.
Regards,
DiGiTSS Team
#13 by Jick on March 29, 2012 - 2:38 pm
Quote
Nice post mate!
#14 by Charles Hoens on July 27, 2012 - 8:19 am
Quote
I need some help! I've been studying this code, but can't seem to get the right answer. I need to compare two times. If one is larger than another return true, if not false. I need to send the function a time and then compare that time to the internal time, then return the answer.
How can this be done?
#15 by supraja on November 29, 2012 - 8:26 pm
Quote
Thanks for posting…………