getTimeDifference 两个时间差
更新: 1970/1/1 字数: 0 字 时长: 0 分钟
说明
getTimeDifference
是一个计算两个日期之间差异的函数。它可以返回指定单位下的时间差,或者返回一个包含多种时间单位差异的对象。
参数
参数名称 | 类型 | 说明 | 可选值 | 默认值 |
---|---|---|---|---|
time1 | string / number / Date | 第一个时间,可以是以下类型之一: - string :日期时间字符串 - number :时间戳(秒或毫秒)- Date :JavaScript Date 对象 | — | — |
time2 | string / number / Date | 第二个时间,可以是以下类型之一: - string :日期时间字符串 - number :时间戳(秒或毫秒)- Date :JavaScript Date 对象 | — | — |
unit | string (可选) | 时间单位,可选值包括: - '年' - '月' - '日' - '小时' - '分钟' - '秒' - '毫秒' | '年' , '月' , '日' , '小时' , '分钟' , '秒' , '毫秒' | 如果未提供此参数,则函数将返回一个包含多个时间单位的对象。 |
返回值
- 如果没有提供
unit
参数,函数返回一个包含以下属性的对象:years
: 年数months
: 月数days
: 天数hours
: 小时数minutes
: 分钟数seconds
: 秒数milliseconds
: 毫秒数
- 如果提供了
unit
参数,函数返回一个数字,表示两个时间点在该单位下的差异。
示例
js
import { getTimeDifference } from 'uviewos';
// 返回详细的时间差对象
const detailedDiff = getTimeDifference('2020-01-01', '2024-09-07');
console.log(detailedDiff);
// 输出类似于:
// {
// years: 4,
// months: 8,
// days: 6,
// hours: 0,
// minutes: 0,
// seconds: 0,
// milliseconds: 0
// }
// 返回特定单位下的时间差
const dayDiff = getTimeDifference('2020-01-01', '2024-09-07', '日');
console.log(dayDiff);
// 输出: 1587