场景一:根据格式化范本返回符合格式要求的日期字符串
/**
*
* @param {*} date 日期类型字符串或时间戳
* @param {*} formatString 格式化范本,如 yyyy-MM-dd hh:mm:ss
* @return 返回符合格式要求的日期字符串
*/
function formatDate(date, formatString) {
if (!formatString) { // 无值则取默认值 yyyy-MM-dd hh:mm:ss
formatString = 'yyyy-MM-dd hh:mm:ss'
}
if (typeof date == 'string') {
date = date.replace(/\-/g, '/') // 兼容ios系统
}
let mydate = new Date(date)
let dateObj = {
y: mydate.getFullYear(),
m: (mydate.getMonth() + 1 < 10) ? '0' + (mydate.getMonth() + 1) : mydate.getMonth() + 1,
d: (mydate.getDate() < 10) ? '0' + mydate.getDate() : mydate.getDate(),
h: (mydate.getHours() < 10) ? '0' + mydate.getHours() : mydate.getHours(),
min: (mydate.getMinutes() < 10) ? '0' + mydate.getMinutes() : mydate.getMinutes(),
s: (mydate.getSeconds() < 10) ? '0' + mydate.getSeconds() : mydate.getSeconds()
}
let res = formatString.replace(/yyyy/g, dateObj.y)
res = res.replace(/MM/g, dateObj.m)
res = res.replace(/dd/g, dateObj.d)
res = res.replace(/hh/g, dateObj.h)
res = res.replace(/mm/g, dateObj.min)
res = res.replace(/ss/g, dateObj.s)
return res
}
场景二:根据日期返回基于当前时间的中文描述:语义化的时间描述:'n月前'/'n周前'/'n天前'/'n小时前'/'n分钟前'/'刚刚'
/**
*
* @param {*} date 日期类型字符串或时间戳
* @return 返回日期基于当前时间的中文描述:语义化的时间描述:'n月前'/'n周前'/'n天前'/'n小时前'/'n分钟前'/'刚刚'
*/
function formatDateToChineseDesc(date) {
let mydate = Math.round(new Date(date).getTime() / 1000)
let min = 60,
hour = min * 60,
day = hour * 24,
month = day * 30,
year = month * 12
let diffVal = Math.round(new Date().getTime() / 1000) - mydate
let diffMonth = diffVal / month,
diffWeek = diffVal / (7 * day),
diffDay = diffVal / day,
diffHour = diffVal / hour,
diffMinute = diffVal / min
if (diffMonth > 3) {
mydate = new Date(date)
return `${mydate.getFullYear()}-${mydate.getMonth() + 1}-${mydate.getDate()}`
}
if (diffMonth > 1) {
return `${parseInt(diffMonth)}月前`
}
if (diffWeek > 1) {
return `${parseInt(diffWeek)}周前`
}
if (diffDay > 1) {
return `${parseInt(diffDay)}天前`
}
if (diffHour > 1) {
return `${parseInt(diffHour)}小时前`
}
if (diffMinute > 1) {
return `${parseInt(diffMinute)}分钟前`
}
return '刚刚'
}
判断目标日期是否处于活动日期范围内(开始时间与结束时间)
/**
*
* @param {*} startDate 开始时间 endDate 结束时间 targetDate 目标时间
* @return 返回是否在时间区间 true or false
*
*/
function targetDateInDateBetwen(startDate, endDate, targetDate) {
//如果时间格式是正确的,那下面这一步转化时间格式就可以不用了
if (typeof startDate == 'string') {
startDate = startDate.replace(/-/g, "/")
}
if (typeof endDate == 'string') {
endDate = endDate.replace(/-/g, "/")
}
if (typeof targetDate == 'string') {
targetDate = targetDate.replace(/-/g, "/")
}
var dateBegin = new Date(startDate);
var dateEnd = new Date(endDate);
var dateTarget = new Date(targetDate);
var beginDiff = dateTarget.getTime() - dateBegin.getTime(); //时间差的毫秒数
var beginDayDiff = Math.floor(beginDiff / (24 * 3600 * 1000)); //计算出相差天数
var endDiff = dateEnd.getTime() - dateTarget.getTime(); //时间差的毫秒数
var endDayDiff = Math.floor(endDiff / (24 * 3600 * 1000)); //计算出相差天数
if (endDayDiff < 0) {
//已过期
return false;
}
if (beginDayDiff < 0) {
//没到开始时间
return false;
}
if (endDayDiff == 0) {
return true;
}
if (beginDayDiff == 0) {
return true;
}
return true;
}
根据活动结束时间计算距离活动结束仅剩x天x小时x分钟
/**
*
* @param {*} endTime 活动结束时间戳
* @return 返回xx天xx小时xx分钟,用于显示距离活动结束仅剩xxxxxxx的场景
*/
function formatDate2EndText(endTime) {
if (typeof endTime == 'string') {
endTime = endTime.replace(/\-/g, '/')
}
let mydate = Math.round(new Date(endTime).getTime() / 1000)
let diffVal = mydate - Math.round(new Date().getTime() / 1000)
if (diffVal < 0) return ''
let min = 60,
hour = min * 60,
day = hour * 24
let diffDay = parseInt(diffVal / day)
let diffHour = parseInt((diffVal - diffDay * day) / hour)
let diffMin = parseInt((diffVal - diffDay * day - diffHour * hour) / min)
let str = ''
if (diffDay > 0) str += `${diffDay}天`
if (diffHour > 0) str += `${diffHour}小时`
if (diffMin > 0) str += `${diffMin}分钟`
return str
}
一般用于显示阅读量/点赞量的显示,比如抖音的场景 338.6w 点赞
/**
*
* @param {*} num 具体数字
* @param {*} languageType 'cn'返回中文'万'字,'en'返回英文w。默认使用英文w
* @return 1万内显示具体数字,10万内精确到小数点后1位,大于10万显示X万+
*/
function formatNumber(num, languageType) {
/** */
if (!languageType) {
languageType = 'en'
}
if (num < 10000) return num
if (num > 100000) {
let n = parseInt(num / 10000)
return n + (languageType == 'cn' ? '万+' : 'w+')
}
let wan = num / 10000
let max = parseInt(wan)
let min = parseInt((num - max * 10000) / 1000)
return `${max}.${min}${languageType == 'cn'?'万':'w'}`
}