获取从当前时间到指定日期的间隔时间
function getChIntv ( putStr) {
var targetTime = new Date(putStr);
var currentTime = new Date();
var interval = currentTime - targetTime
var totalSecond = Math.floor(interval/1000)
var second = totalSecond%60
var totalminute = Math.floor(totalSecond/60)
var minute = totalminute % 60
var totalhour = Math.floor(totalminute/60)
var hours = totalhour % 24
var day = Math.floor(totalhour / 24)
return day + "days" + hours + "hours" + minute + "minutes" + second + "seconds";
}
var result = getChIntv("2017,08,01") //"25days15hours5minutes34seconds"
把hh-mm-dd格式数字日期改成中文日期
function getChsDate (putstr) {
var dict = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七",
"十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十",
"三十一"]
var arr = putstr.split('-')
var arrYear = arr[0].split('')
var strYear = ""
for (var i=0; i<4; i++) {
strYear += dict[parseInt(arrYear[i])]
}
var strMonth = dict[parseInt(arr[1])]
var strDate = dict[parseInt(arr[2])]
return strYear + '年' + strMonth + '月' + strDate + '日'
}
var result = getChsDate("2017-08-01")
写一个函数,参数为时间对象毫秒数的字符串格式,返回值为字符串。假设参数为时间对象毫秒数t,根据t的时间分别返回如下字符串:
刚刚( t 距当前时间不到1分钟时间间隔)
3分钟前 (t距当前时间大于等于1分钟,小于1小时)
8小时前 (t 距离当前时间大于等于1小时,小于24小时)
3天前 (t 距离当前时间大于等于24小时,小于30天)
2个月前 (t 距离当前时间大于等于30天小于12个月)
8年前 (t 距离当前时间大于等于12个月)
function friendlyDate(time) {
var targetTime = new Date(time)
var currentTime = Date.now()
var offset = currentTime - targetTime
var minute = Math.floor(offset/1000/60)
if (offset < 1) {
return "刚刚"
} else if(minute < 60){
return "三分钟前"
} else if(minute < 1440){
return "八小时前"
} else if (minute < 43200){
return "三天前"
} else if (minute < 518400){
return "两个月前"
} else{
return "八年前"
}
}
var str = friendlyDate('1484286699422')
网友评论