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

42 行
1.7 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 {
/**
*
* @returns
*/
static getTimeMillis() {
return new Date().getTime()
}
/**
*
* @param formats
* 'yyyy年MM月dd日'
*/
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]);
}
}