Files
bbq/src/app/shared/utils/date.util.ts
wangshiming 0a31c9eb4e 车辆对接
2021-12-20 17:18:40 +08:00

104 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @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;
}
}
}