[封装]javascript输入时间返回距离当前的时间差
Posted 帅气的黑桃J
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[封装]javascript输入时间返回距离当前的时间差相关的知识,希望对你有一定的参考价值。
简介
封装后便于调用
快速上手
- 函数封装
// 解析dateTime距离现在的时间
// dateTime的格式为2020-05-05 15:05:05
function getDateDiff(dateTime)
var dateTimeStamp = new Date(dateTime.replace(/-/g, '/')).getTime()
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
var result = '';
if (diffValue < 0)
return;
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1)
result = "" + parseInt(monthC) + "月前";
else if (weekC >= 1)
result = "" + parseInt(weekC) + "周前";
else if (dayC >= 1)
result = "" + parseInt(dayC) + "天前";
else if (hourC >= 1)
result = "" + parseInt(hourC) + "小时前";
else if (minC >= 1)
result = "" + parseInt(minC) + "分钟前";
else
result = "刚刚";
console.log(`[info] $result`)
return result
以上是关于[封装]javascript输入时间返回距离当前的时间差的主要内容,如果未能解决你的问题,请参考以下文章