Skip to content

Instantly share code, notes, and snippets.

@simplelife7
Last active December 22, 2015 15:29
Show Gist options
  • Save simplelife7/6492863 to your computer and use it in GitHub Desktop.
Save simplelife7/6492863 to your computer and use it in GitHub Desktop.
【JS】时间对比函数
//Get days and other datetime
//diffrence two datetime
// date1 :更早的日期 小的日期
// date2 :后面的日期 大的日期
// 返回两个时间差的天数小时数分数秒数和毫秒数
function DiffLong(datestr1,datestr2)
{
var date1 = new Date(Date.parse(datestr1));
var date2 = new Date(Date.parse(datestr2));
var datetimeTemp;
var isLater = true;
if(date1.getTime() > date2.getTime())
{
isLater = false;
datetimeTemp = date1;
date1 = date2;
date2 = datetimeTemp;
}
difference= date2.getTime() - date1.getTime();
thisdays = Math.floor(difference /(1000* 60 * 60*24));
difference = difference - thisdays * (1000*60*60*24);
thishours = Math.floor(difference/(1000* 60*60));
difference = difference - thishours * (1000*60*60);
thisminutes = Math.floor(difference/(1000* 60));
difference = difference - thisminutes * (1000*60);
thisseconds = Math.floor(difference/1000);
difference = difference - thisseconds * 1000;
thismillseconds = difference;
//document.write(date1 + 'only ' + thisdays + ' days until '+ date2 +'<P>');
//document.write(date1 + 'only ' + thishours + ' hours until '+ date2 +'<P>');
//document.write(date1 + 'only ' + thisminutes + ' minutes until '+ date2 +'<P>');
//document.write(date1 + 'only ' + thisseconds + ' seconds until '+ date2 +'<P>');
//document.write(date1 + 'only ' + thismillseconds + ' millisconds until '+ date2 +'<P>');
var strRet = thisdays + ' Days ' + thishours + ' hours ' +thisminutes + ' minutes ' + thisseconds + ' seconds ' + thismillseconds + ' milliseconds';
return strRet;
}
DiffLong('2013-09-24 00:00', '2013-09-25 00:00')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment