73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
export default class SKDateUtil {
|
|
|
|
static toString(value: Date): string {
|
|
let year = value.getFullYear();
|
|
let month = (value.getMonth() + 1).toString();
|
|
let day = value.getDate().toString();
|
|
let result = `${year}-${month}-${day}`;
|
|
return result;
|
|
}
|
|
|
|
static dateBy(value: string): Date {
|
|
if (value == null) {
|
|
return null;
|
|
}
|
|
let params = value.split("-");
|
|
if (params.length != 3) {
|
|
return null;
|
|
}
|
|
let year = parseInt(params[0]);
|
|
let month = parseInt(params[1]);
|
|
let day = parseInt(params[2]);
|
|
let result = new Date(year, month - 1, day);
|
|
return result;
|
|
}
|
|
|
|
static atRange(start: string, end: string): boolean {
|
|
let now = new Date();
|
|
let startDate = this.dateBy(start);
|
|
if (startDate) {
|
|
if (now < startDate) {
|
|
return false;
|
|
}
|
|
}
|
|
let endDate = this.dateBy(end);
|
|
if (endDate) {
|
|
if (now > endDate) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
// 格式化消息时间
|
|
static formatMsgTime(last: Date): string {
|
|
let year = last.getFullYear();
|
|
let month = last.getMonth() + 1;
|
|
let day = last.getDate();
|
|
let hour = last.getHours();
|
|
let minute = last.getMinutes();
|
|
let now = new Date();
|
|
let now_time = now.getTime();
|
|
let milliseconds = 0;
|
|
let result;
|
|
milliseconds = now_time - last.getTime();
|
|
if (milliseconds <= 1000 * 60 * 1) {
|
|
result = '刚刚';
|
|
}
|
|
else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
|
|
result = Math.round((milliseconds / (1000 * 60))) + '分钟前';
|
|
}
|
|
else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
|
|
result = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
|
|
}
|
|
else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
|
|
result = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
|
|
}
|
|
else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
|
|
result = month + '-' + day + ' ' + hour + ':' + minute;
|
|
} else {
|
|
result = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
|
|
}
|
|
return result;
|
|
};
|
|
} |