You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
3.7 KiB

// 时间日期格式化
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 几天前时间转换
function getDateDiff(dateTimeStamp) {
var result;
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;
if (diffValue < 0) {
return;
}
// 数值补0方法
var zero = function(value) {
if (value < 10) {
return '0' + value;
}
return value;
};
var date = new Date(dateTimeStamp);
// 今年时间
var CurrentDateTime = zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
// 过了一年时间显示
var dateTimeed = date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
// console.log('---------------')
// console.log(minute)
// console.log(hour)
// console.log(day)
// console.log(halfamonth)
// console.log(month)
// console.log(now)
// console.log(diffValue)
// console.log('-----------------')
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
// console.log('。。。。。。。。')
// console.log(monthC)
// console.log(weekC)
// console.log(dayC)
// console.log(hourC)
// console.log(minC)
// console.log('。。。。。。。。。')
if(monthC > 12){
result = dateTimeed;
} else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前";
} else if (dayC >= 1 && dayC < 2) {
result = '昨天';
}else if (dayC >= 2 && dayC < 3) {
result = '前天';
} else if (dayC > 3) {
result = CurrentDateTime;
} else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小时前";
} else if (minC >= 1) {
result = "" + parseInt(minC) + "分钟前";
} else {
result = "刚刚";
}
return result;
};
function formatDate(dateTime, formatStr) {
var converted = Date.parse(dateTime)
var thisDateTime = new Date(converted)
if (thisDateTime) {
let str = formatStr
const Week = ['日', '一', '二', '三', '四', '五', '六']
str = str.replace(/yyyy|YYYY/, thisDateTime.getFullYear())
str = str.replace(/yy|YY/, (thisDateTime.getYear() % 100) > 9 ? (thisDateTime.getYear() % 100).toString() : '0' + (thisDateTime.getYear() % 100))
str = str.replace(/MM/, (thisDateTime.getMonth() + 1) > 9 ? (thisDateTime.getMonth() + 1).toString() : '0' + (thisDateTime.getMonth() + 1))
str = str.replace(/M/g, thisDateTime.getMonth())
str = str.replace(/w|W/g, Week[thisDateTime.getDay()])
str = str.replace(/dd|DD/, thisDateTime.getDate() > 9 ? thisDateTime.getDate().toString() : '0' + thisDateTime.getDate())
str = str.replace(/d|D/g, thisDateTime.getDate())
str = str.replace(/hh|HH/, thisDateTime.getHours() > 9 ? thisDateTime.getHours().toString() : '0' + thisDateTime.getHours())
str = str.replace(/h|H/g, thisDateTime.getHours())
str = str.replace(/mm/, thisDateTime.getMinutes() > 9 ? thisDateTime.getMinutes().toString() : '0' + thisDateTime.getMinutes())
str = str.replace(/m/g, thisDateTime.getMinutes())
str = str.replace(/ss|SS/, thisDateTime.getSeconds() > 9 ? thisDateTime.getSeconds().toString() : '0' + thisDateTime.getSeconds())
str = str.replace(/s|S/g, thisDateTime.getSeconds())
return str
} else { return formatStr }
}
module.exports = {
formatTime,
getDateDiff,
formatDate
}