TIME
创建传感器
const time = hmSensor.createSensor(hmSensor.id.TIME)
time 实例
time: object
属性 | 说明 | 类型 |
---|---|---|
utc | 时间戳, 1970 年 1 月 1 日至今的毫秒数 | number |
year | 年 | number |
month | 月 | number |
day | 日 | number |
hour | 小时 | number |
minute | 分钟 | number |
second | 秒 | number |
week | 星期 1 - 7 ,1 代表星期 一 | 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()
}
}
})