getCountdown时间倒计时
更新: 1970/1/1 字数: 0 字 时长: 0 分钟
说明
getCountdown 是一个实现倒计时功能的函数,可以实时显示到目标日期剩余的年、月、周、天、小时、分钟、秒。
参数说明
| 参数 | 类型 | 描述 |
|---|---|---|
targetDate | Date | 目标日期,可以是 JavaScript Date 对象 |
unit | String (可选) | 时间单位,可选值包括:year、month、week、day、hour、minute、second |
返回值
- 如果没有提供
unit参数,函数返回一个包含以下属性的对象:years: 年数months: 月数weeks: 周数days: 天数hours: 小时数minutes: 分钟数seconds: 秒数
- 如果提供了
unit参数,函数返回一个数字,表示到目标日期在该单位下的剩余时间。
示例
js
import { getCountdown } from 'uviewos';
// 获取到目标日期的倒计时对象
const targetDate = new Date('2024-12-31');
const countdown = getCountdown(targetDate);
console.log(countdown);
// 输出类似于:
// {
// years: 0,
// months: 3,
// weeks: 1,
// days: 24,
// hours: 0,
// minutes: 0,
// seconds: 0
// }
// 获取到目标日期在特定单位下的剩余时间
const daysRemaining = getCountdown(targetDate, '天');
console.log(daysRemaining);
// 输出: 112
