js日期相减

// var date = "2019-06-24 11:55:00",date2 = "2019-06-24 14:05:00";


function dateLess(fromDate, toDate){ var date = new Date(fromDate); var date2 = new Date(toDate); var s1 = date.getTime(),s2 = date2.getTime(); var total = (s2 - s1)/1000; var day = parseInt(total / (24*60*60));//计算整数天数 var afterDay = total - day*24*60*60;//取得算出天数后剩余的秒数 var hour = parseInt(afterDay/(60*60));//计算整数小时数 var afterHour = total - day*24*60*60 - hour*60*60;//取得算出小时数后剩余的秒数 var min = parseInt(afterHour/60);//计算整数分 var afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒数 if(day === 0){ if(hour === 0){ return min+"分"+afterMin+"秒"; }else{ return hour+"时"+min+"分"+afterMin+"秒"; } }else{ return day+"天"+hour+"时"+min+"分"+afterMin+"秒"; } }

 

posted @ 2019-06-24 14:41  decrypt  阅读(1792)  评论(0编辑  收藏  举报