HarmonyOS 鸿蒙Next Date相关问题
HarmonyOS 鸿蒙Next Date相关问题
constructor(appointTimeStr:
string
)
{
this.appointTimeStr = appointTimeStr
const appointDate = new Date(appointTimeStr)
const appointTimeStamp: number = appointDate.getTime()
const currentDate = new Date()
const currentTimeStamp: number = currentDate.getTime()
let timeStampDif: number = appointTimeStamp - currentTimeStamp
if (timeStampDif <= 0) {
this.isToTime = true;
}
//24小时以上 显示直播时间;以内显示直播倒计时
if (timeStampDif <= 0 || timeStampDif > 24 * 60 * 60 * 1000) {
this.isCountdown = false;
this.descStr = ‘直播开始时间’;
this.month = appointDate.getUTCMonth().toString();
this.day = appointDate.getUTCDay().toString();
const hourStr = appointDate.getUTCHours().toString();
const minuteStr = appointDate.getUTCMinutes().toString();
this.hourMinuteStr = ${hourStr.padStart(<span class="hljs-number">2</span>, <span class="hljs-string">'0'</span>)}:${minuteStr.padStart(<span class="hljs-number">2</span>, <span class="hljs-string">'0'</span>)}
} else {
this.isCountdown = true;
this.descStr = ‘距离直播开始还有’;
this.countdownStr = CommonUtils.timeToClockStr(Math.max(Math.floor(timeStampDif / 1000), 0))
}
}
2 回复
1、Date中的月份默认是从0开始,可以通过date.getMonth() + 1获取当前对应月份;
2、使用date.getDay()获取的是当前在本周内的第几天,获取当前日期值可以通过date.getDate()获取
具体如下:
this.month = (appointDate.getMonth()+1).toString();
this.day = appointDate.getDate().toString();
const hourStr = appointDate.getHours().toString();
const minuteStr = appointDate.getMinutes().toString();
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>