js日付は何時間前、何分前、何秒前
3119 ワード
プロジェクトの例https://github.com/jaywcjlove/date.js
ソース:
例:
型パラメータに変更しましたが、そうでしょう.
参考までに!
ソース:
;(function(window){
/**
* [dateDiff ]
* @param {[type=Number]} hisTime [ , ]
* @param {[type=Number]} nowTime [ , ]
* @return {[string]} [string]
*/
var dateDiff = function(hisTime,nowTime){
var now =nowTime?nowTime:new Date().getTime(),
diffValue = now - hisTime,
result='',
minute = 1000 * 60,
hour = minute * 60,
day = hour * 24,
halfamonth = day * 15,
month = day * 30,
year = month * 12,
_year = diffValue/year,
_month =diffValue/month,
_week =diffValue/(7*day),
_day =diffValue/day,
_hour =diffValue/hour,
_min =diffValue/minute;
if(_year>=1) result=parseInt(_year) + " ";
else if(_month>=1) result=parseInt(_month) + " ";
else if(_week>=1) result=parseInt(_week) + " ";
else if(_day>=1) result=parseInt(_day) +" ";
else if(_hour>=1) result=parseInt(_hour) +" ";
else if(_min>=1) result=parseInt(_min) +" ";
else result=" ";
return result;
}
window.dateDiff = dateDiff
})(window);
例:
:
dateDiff(Timestamp,now Timestamp)
Timestamp:
dateDiff(1411430400000,1421313395359)
//=>"3 "
dateDiff(new Date('1987-04-03').getTime())
//=>"28 "
型パラメータに変更しましたが、そうでしょう.
;(function(window){
/**
* [dateDiff ]
* @param {[type=Number]} hisTime [ , ]
* @param {[type=Number]} nowTime [ , ]
* @return {[string]} [string]
*/
var dateDiff = function(hisTime,nowTime){
if(!arguments.length) return '';
var arg = arguments,
now =arg[1]?arg[1]:new Date().getTime(),
diffValue = now - arg[0],
result='',
minute = 1000 * 60,
hour = minute * 60,
day = hour * 24,
halfamonth = day * 15,
month = day * 30,
year = month * 12,
_year = diffValue/year,
_month =diffValue/month,
_week =diffValue/(7*day),
_day =diffValue/day,
_hour =diffValue/hour,
_min =diffValue/minute;
if(_year>=1) result=parseInt(_year) + " ";
else if(_month>=1) result=parseInt(_month) + " ";
else if(_week>=1) result=parseInt(_week) + " ";
else if(_day>=1) result=parseInt(_day) +" ";
else if(_hour>=1) result=parseInt(_hour) +" ";
else if(_min>=1) result=parseInt(_min) +" ";
else result=" ";
return result;
}
window.dateDiff = dateDiff
})(window);
参考までに!