元服务生态有数量多,商业模式差异大等特点,为了构建纯净元服务生态和商业目标达成,需要构建有别于应用的API使用策略和管控规则,所以有部分API在元服务中不可用的情况。元服务支持的API是应用支持API的子集,具体可以参考:如何查看支持元服务的API。
元服务可以通过Date.now()或者new Date().getTime()获取当前时间戳并对时间戳处理后展示当前系统时间,也可通过TextClock组件显示当前系统时间。
@Entry @Component struct Index { build() { Column() { Button('获取当前时间') .onClick(() => { // 获取当前时间戳 let timestamp = Date.now(); console.info('当前时间戳:' + timestamp); console.info('当前时间:' + this.getDateStringWithTimeStamp(timestamp)); }) } } getDateStringWithTimeStamp(timestamp: number): string { let date = new Date(timestamp); const year = date.getFullYear(); const month = ('0' + (date.getMonth() + 1)).slice(-2); const day = ('0' + date.getDate()).slice(-2); const hour = date.getHours(); const min = date.getMinutes(); const sec = date.getSeconds(); return `${year}年${month}月${day}日${hour}时${min}分${sec}秒`; } }
打印日志示例:
05-14 11:26:00.530 56803-56803 A03D00/micserv...4730733/JSAPP com.atomi...54730733 I 当前时间戳:1747193160530 05-14 11:26:00.531 56803-56803 A03D00/micserv...4730733/JSAPP com.atomi...54730733 I 当前时间:2025年05月14日11时26分0秒
@Entry @Component struct Second { @State accumulateTime: number = 0; controller: TextClockController = new TextClockController(); build() { Column() { TextClock({ timeZoneOffset: -8, controller: this.controller }) .format('yyyy年M月d日 HH:mm:ss') .onDateChange((value: number) => { this.accumulateTime = value; }) Text('accumulateTime is ' + this.accumulateTime) } .margin({ top: 100 }) } }