项目初始化
This commit is contained in:
56
src/app/shared/utils/date.util.ts
Normal file
56
src/app/shared/utils/date.util.ts
Normal file
@ -0,0 +1,56 @@
|
||||
export class EADateUtil {
|
||||
/**
|
||||
* @description 时间戳转换日期
|
||||
* @param timestamp 时间戳
|
||||
* @returns 时间戳对应的日期
|
||||
*/
|
||||
static timestampToDate(timestamp: number): Date {
|
||||
let date;
|
||||
// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
|
||||
if (timestamp.toString().length === 10) {
|
||||
date = new Date(timestamp * 1000);
|
||||
} else {
|
||||
date = new Date(timestamp);
|
||||
}
|
||||
|
||||
const Y = date.getFullYear();
|
||||
const M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
|
||||
const D = date.getDate();
|
||||
const h = date.getHours();
|
||||
const m = date.getMinutes();
|
||||
const s = date.getSeconds();
|
||||
|
||||
const dateStr = `${Y}-${M}-${D} ${h}:${m}:${s}`;
|
||||
return new Date(dateStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 日期相加
|
||||
* @param start 开始日期
|
||||
* @param intervalMS 间隔秒数(毫秒)
|
||||
* @returns 相加后的日期
|
||||
*/
|
||||
static dateAdd(start: Date, intervalMS: number): Date {
|
||||
return this.timestampToDate(start.getTime() + intervalMS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 日期相减
|
||||
* @param start 开始日期
|
||||
* @param intervalMS 时间间隔(毫秒)
|
||||
* @returns 相减后的日期
|
||||
*/
|
||||
static dateMinus(start: Date, intervalMS: number): Date {
|
||||
return this.timestampToDate(start.getTime() - intervalMS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 计算两个日期的时间差
|
||||
* @param start 开始日期
|
||||
* @param end 结束日期
|
||||
* @returns 两时间相差的毫秒数
|
||||
*/
|
||||
static dateDiff(start: Date, end: Date): number {
|
||||
return start.getTime() - end.getTime();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user