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.

76 lines
2.0 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 formatDate = date =>{
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return `${[year, month, day].map(formatNumber).join('-')}`
}
const formatTime_a = date =>{
const hour = date.getHours()
const minute = date.getMinutes()
return `${[hour, minute].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
const getend = later =>{
let currentdate = new Date();
currentdate.setDate(currentdate.getDate() + later);
return formatDate(currentdate)
}
function getDateDiff(dateTime) {
let dateTimeStamp = new Date(dateTime).getTime();
let result = '';
let minute = 1000 * 60;
let hour = minute * 60;
let day = hour * 24;
let halfamonth = day * 15;
let month = day * 30;
let year = day * 365;
let now = new Date().getTime();
let diffValue = now - dateTimeStamp;
if (diffValue < 0) {
return;
}
let monthEnd = diffValue / month;
let weekEnd = diffValue / (7 * day);
let dayEnd = diffValue / day;
let hourEnd = diffValue / hour;
let minEnd = diffValue / minute;
let yearEnd = diffValue / year;
if (yearEnd >= 1) {
result = dateTime;
} else if (monthEnd >= 1) {
result = dateTime;
} else if (dayEnd >= 4) {
result = dateTime;
} else if ((dayEnd >= 1)&&(dayEnd<=3)){
result = "" + parseInt(dayEnd) + "天前";
} else if (hourEnd >= 1) {
result = "" + parseInt(hourEnd) + "小时前";
} else if (minEnd >= 1) {
result = "" + parseInt(minEnd) + "分钟前";
} else {
result = "刚刚";
}
return result;
}
module.exports = {
formatTime,
formatDate,
getend,
formatTime_a,
getDateDiff: getDateDiff
}