export const zero = (value: number | string) => value.toString().length === 1 ? `0${value}` : value; export default class DateUtil{ constructor() { } public static getCurrentDateTime(format?:string):string { return this.toDateString(new Date(), format); } public static toDateString(date:Date, format?:string):string{ if (format == null || format == undefined) { format = "yyyy-MM-DD"; } return this.toDateFormat(date, format); } public static toDateTimeString(date:Date, format?:string):string{ if (format == null || format == undefined) { format = "yyyy-MM-DD H:i:s"; } return this.toDateFormat(date, format); } public static toDateFormat(date: Date, format: string) { return format.replace(/(yyyy|mm|dd|MM|DD|H|i|s)/g, (t: string): any => { switch (t) { case "yyyy": return date.getFullYear(); case "mm": return date.getMonth() + 1; case "dd": return date.getDate(); case "MM": return zero(date.getMonth() + 1); case "DD": return zero(date.getDate()); case "H": return zero(date.getHours()); case "i": return zero(date.getMinutes()); case "s": return zero(date.getSeconds()); default: return ""; } }); } public static addMonth(date:Date, gap:number):Date{ let yy1:number = date.getFullYear(); let mm1:number = date.getMonth(); let dd1:number = date.getDate(); return new Date(yy1, mm1+gap, dd1); } public static addDay(date:Date, gap:number):Date{ let yy1:number = date.getFullYear(); let mm1:number = date.getMonth(); let dd1:number = date.getDate(); return new Date(yy1, mm1, dd1+gap); } }