JS時間(日付)比較または減算
1882 ワード
/JAVASCRIPT , , ,
// ( )
function dateDiff(date1, date2){
var type1 = typeof date1, type2 = typeof date2;
if(type1 == 'string')
date1 = stringToTime(date1);
else if(date1.getTime)
date1 = date1.getTime();
if(type2 == 'string')
date2 = stringToTime(date2);
else if(date2.getTime)
date2 = date2.getTime();
return (date1 - date2) / 1000;//
}
// Time(dateDiff)
function stringToTime(string){
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1)-1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime();
}
// dateDiff("2009-10-10 19:00:00","2009-10-10 18:00:00")
//Get days and other datetime
//diffrence two datetime
// date1 :
// date2 :
//
function DiffLong(datestr1, datestr2) {
var date1 = new Date(Date.parse(datestr1.replace(/-/g, "/")));
var date2 = new Date(Date.parse(datestr2.replace(/-/g, "/")));
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));
var strRet = thisdays + ' ' + thishours + ' ';
return strRet;
}