鸿蒙日期格式工具封装及使用
封装 DataFormat.ets
/**
 * @param {Date} value
 * @return {string}
 * @example
 * @description 格式化日期
 */
export class DataFormat {
  dateFormat(value: Date): string {
    const year = value.getFullYear();
    const month = value.getMonth() + 1;
    const day = value.getDate();
    return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  }
}
export default new DataFormat();
用法
import formatutil from '../util/DataForat';
@Entry
@Component
struct Index {
  @State title: string =formatutil.dateFormat(new Date())
  build() {
    RelativeContainer() {
      Text(this.title)
    }
    .height('100%')
    .width('100%')
  }
}效果

