Skip to main content
版本:v1.0

TIME

创建传感器

const time = hmSensor.createSensor(hmSensor.id.TIME)

time 实例

time: object

属性说明类型
utc时间戳, 1970 年 1 月 1 日至今的毫秒数number
yearnumber
monthnumber
daynumber
hour小时number
minute分钟number
secondnumber
week星期 1 - 71 代表星期一number
is24Hour是否 24 小时制boolean
format_hour当前时间制下的小时number
lunar_year中国农历年份number
lunar_month中国农历月份number
lunar_day中国农历日期number
lunar_festival中国农历节日,如果当天无节日返回 INVALID 字符串string
lunar_solar_term中国农历节气,如果当天无节气返回 INVALID 字符串string
solar_festival公历节日,如果当天无节日返回 INVALID 字符串string
警告

中国农历和节日相关属性,仅当手表语言设置为中文时,才会返回有意义的值,否则为 undefined

time.getLunarMonthCalendar

获取中国农历月历信息

const lunar_month_cal = time.getLunarMonthCalendar()

for (let i = 0; i < lunar_month_cal.day_count; i++) {
console.log('lunar_day : ' + lunar_month_cal.lunar_days_array[i])
}

time.getShowFestival

获取当天显示的节日(优先级依次是 公历节日、中国农历节日、中国农历节气)

const current_festival = time.getShowFestival()

注册传感器实例回调事件

time.addEventListener(event, callback: Callback)

MINUTEEND 事件

每分钟结束时产生事件

event 值

time.event.MINUTEEND

Callback

() => void

DAYCHANGE 事件

每天结束时产生事件

event 值

time.event.MINUTEEND

Callback

() => void

完整代码示例

class TextByLine {
constructor(params) {
const { text = '', y = undefined, line = 0 } = params

this.text = text
this.y = y
this.line = line
this.y_computed = Number.isInteger(this.y) ? this.y : px(this.line * 60 + 120)
}

render() {
return hmUI.createWidget(hmUI.widget.TEXT, {
x: px(0),
y: this.y_computed,
w: px(480),
h: px(46),
color: 0xffffff,
text_size: px(20),
align_h: hmUI.align.CENTER_H,
align_v: hmUI.align.CENTER_V,
text_style: hmUI.text_style.NONE,
text: this.text
})
}
}

Page({
build() {
const time = hmSensor.createSensor(hmSensor.id.TIME)

new TextByLine({
text: `utc:${time.utc};year:${time.year};month:${time.month};day:${time.day}`,
line: 0
}).render()

new TextByLine({
text: `hour:${time.hour};minute:${time.minute};second:${time.second};week:${time.week}`,
line: 1
}).render()

new TextByLine({
text: `format_hour:${time.format_hour};is24Hour:${time.is24Hour}`,
line: 2
}).render()

new TextByLine({
text: `lunar_year:${time.lunar_year};lunar_month:${time.lunar_month};`,
line: 3
}).render()

new TextByLine({
text: `lunar_year:${time.lunar_year};lunar_month:${time.lunar_month};lunar_day:${time.lunar_day}`,
line: 4
}).render()

new TextByLine({
text: `lunar_festival:${time.lunar_festival};lunar_solar_term:${time.lunar_solar_term};solar_festival:${time.solar_festival}`,
line: 5
}).render()

new TextByLine({
text: `getShowFestival:${time.getShowFestival()}`,
line: 6
}).render()

const lunar_month_cal = time.getLunarMonthCalendar()

for (let i = 0; i < lunar_month_cal.day_count; i++) {
new TextByLine({
text: `index:${i};lunar_day:${lunar_month_cal.lunar_days_array[i]}`,
line: 7 + i
}).render()
}
}
})