104 lines
3.0 KiB
TypeScript
104 lines
3.0 KiB
TypeScript
/*
|
||
* @Author: your name
|
||
* @Date: 2021-11-29 10:04:12
|
||
* @LastEditTime: 2021-12-20 16:55:59
|
||
* @LastEditors: Please set LastEditors
|
||
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||
* @FilePath: \tms-obc-web\src\app\shared\utils\date.util.ts
|
||
*/
|
||
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();
|
||
}
|
||
/**
|
||
* @description 国际时间转换成YYYY-MM-DD 00:00:00
|
||
* @param start 开始日期
|
||
* @returns YYYY-MM-DD 00:00:00
|
||
*/
|
||
static yearToDateTime(start: Date): number {
|
||
console.log('222')
|
||
console.log(start)
|
||
let Dates : any='';
|
||
if(typeof(start) !== 'string') {
|
||
var c = new Date(start);
|
||
Dates = c.getFullYear() + '-' + this.addPreZero(c.getMonth() + 1) + '-' + this.addPreZero(c.getDate()) + ' ' + this.addPreZero(c.getHours()) + ':' + this.addPreZero(c.getMinutes()) + ':' + this.addPreZero(c.getSeconds())
|
||
} else {
|
||
return start;
|
||
}
|
||
return Dates;
|
||
}
|
||
/**
|
||
* @description 国际时间转换YYYY-MM-DD
|
||
* @param start 开始日期
|
||
* @returns YYYY-MM-DD
|
||
*/
|
||
static yearToDate(start: Date): number {
|
||
let Dates : any='';
|
||
if(typeof(start) !== 'string' ) {
|
||
var c = new Date(start);
|
||
Dates = c.getFullYear() + '-' + this.addPreZero(c.getMonth() + 1) + '-' + this.addPreZero(c.getDate());
|
||
} else {
|
||
return start;
|
||
}
|
||
return Dates;
|
||
}
|
||
static addPreZero(num: any) {
|
||
if(num<10){
|
||
return '0'+num;
|
||
} else {
|
||
return num;
|
||
}
|
||
}
|
||
}
|