时间格式化
export function ParseTime (time: number, cFormat: string): string {
if (arguments.length === 0 || !time) {
return '';
};
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
let date: Date;
if (typeof time === 'object') {
date = time;
} else {
if (('' + time).length === 10) {
time = time * 1000;
};
date = new Date(time);
};
const formatObj: any = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
};
const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value: any = formatObj[key];
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] };
if (result.length > 0 && value < 10) {
value = '0' + value
};
return value || 0;
});
return timeStr;
};
获取当前时间往前推几个月
export function handleForwardMonth(month: number) {
var today = new Date()
today.setMonth(today.getMonth()-month)
today.toLocaleDateString()
return new Date(today).getTime();
}
一些常用api
new Date().setDate(1)
new Date().getDate()
new Date(2022, 1, 1).getDate()
new Date(2022, 1, 0).getDate()
其它拓展
export function HandleChatListTime(currentTimer: number, isBackTime = true) {
if (!currentTimer) { return; }
if (isToday(currentTimer)) {
return '今天 ' + (isBackTime? ParseTime(currentTimer, '{h}:{i}') : '');
} else if (isYesterday(currentTimer)) {
return '昨天 ' + (isBackTime ? ParseTime(currentTimer, '{h}:{i}') : '');
} else if (isThatYear(currentTimer)) {
return ParseTime(currentTimer, '{m}月{d}日');
} else {
return ParseTime(currentTimer, '{y}年{m}月{d}日');
};
};
export function isThatYear(time: number) {
const date = new Date(time).getFullYear();
const cur = new Date().getFullYear();
return date === cur;
};
export function isYesterday(time: number) {
const date = new Date();
const today = handleYearMothDay(date);
const todayTime = new Date(today).getTime();
const yesterdayTime = new Date(todayTime - 24 * 60 * 60 * 1000).getTime();
return time < todayTime && yesterdayTime <= time;
};
export function isToday(time: number) {
let data = handleYearMothDay(new Date(time));
let cur = handleYearMothDay(new Date());
return data === cur;
};
export function handleYearMothDay(date: any) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const today = `${year}/${month}/${day}`;
return today;
};
export function handleCountDown(startTime: number, endTime: number) {
var dStr = '', hStr = '', mStr = '', sStr = '';
var times: any = (endTime - startTime) / 1000;
var d = Math.floor(times / 86400);
dStr = d < 10 ? "0" + d : d + '';
var h = parseInt((times / 60 / 60 % 24) + '');
hStr = h < 10 ? "0" + h : h + '';
var m = parseInt((times / 60 % 60) + '');
mStr = m < 10 ? "0" + m : m + '';
var s = parseInt((times % 60) + '');
sStr = s < 10 ? "0" + s : s + '';
return `${hStr}:${mStr}:${sStr}`;
};
export function HandleTalkTime(startTime: number, endTime: number) {
var dStr = '', hStr = '', mStr = '', sStr = '';
var times: any = (endTime - startTime) / 1000;
var d = Math.floor(times / 86400);
dStr = d < 10 ? "0" + d : d + '';
var h = parseInt((times / 60 / 60 % 24) + '');
hStr = h < 10 ? "0" + h : h + '';
var m = parseInt((times / 60 % 60) + '');
mStr = m < 10 ? "0" + m : m + '';
var s = parseInt((times % 60) + '');
sStr = s < 10 ? "0" + s : s + '';
if (dStr !== '00') {
return `${dStr}天${hStr}时${mStr}分${sStr}秒`;
} else if (hStr !== '00') {
return `${hStr}时${mStr}分${sStr}秒`;
} else if (mStr !== '00') {
return `${mStr}分${sStr}秒`;
} else if (sStr !== '00') {
return `${sStr}秒`;
} else {
return '0秒';
};
}