HarmonyOSBaseLibs/src/main/ets/utils/TimeHelper.ts

59 行
2.0 KiB
TypeScript

/**
*
*
* yy yyyy-2015
* MM MM-01
* dd dd-02
* H24 H 0-23 HH-17
* h12 h 1-12 hh-05
* mm mm-19
* ss ss-22
* Yy Week Year YYYY-2015
* DD Y起作用 DD-362
* SS SSS-043
*/
export class TimeHelper {
private constructor() {
}
/**
*
* @returns
*/
static getTimeMillis() {
return new Date().getTime()
}
/**
*
* @param formats
* 'yyyy-MM-dd HH:mm:ss'
*/
static getTime(format: string = 'yyyy-MM-dd HH:mm:ss') {
return TimeHelper.formatDate(new Date(), format)
}
static formatDate(date: Date, format: string) {
const replacements: { [key: string]: string } = {
yyyy: date.getFullYear().toString(),
MM: String(date.getMonth() + 1).padStart(2, "0"),
dd: String(date.getDate()).padStart(2, "0"),
HH: String(date.getHours()).padStart(2, "0"),
mm: String(date.getMinutes()).padStart(2, "0"),
ss: String(date.getSeconds()).padStart(2, "0")
};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, matched => replacements[matched]);
}
/**
*
* @param year
* @param month
* @returns
*/
static getMonthDays(year: number, month: number) {
return new Date(year, month, 0).getDate()
}
}