网站公告

本次更新:新公告样式

详细教程:vitepress.yiov.top

QQ 频道:******(无效二维码)

Skip to content

getTimeDifference 两个时间差

更新: 1970/1/1 字数: 0 字 时长: 0 分钟

说明

getTimeDifference 是一个计算两个日期之间差异的函数。它可以返回指定单位下的时间差,或者返回一个包含多种时间单位差异的对象。

参数

参数名称类型说明可选值默认值
time1string / number / Date第一个时间,可以是以下类型之一:
- string:日期时间字符串
- number:时间戳(秒或毫秒)
- Date:JavaScript Date 对象
time2string / number / Date第二个时间,可以是以下类型之一:
- string:日期时间字符串
- number:时间戳(秒或毫秒)
- Date:JavaScript Date 对象
unitstring (可选)时间单位,可选值包括:
- '年'
- '月'
- '日'
- '小时'
- '分钟'
- '秒'
- '毫秒'
'年', '月', '日', '小时', '分钟', '秒', '毫秒'如果未提供此参数,则函数将返回一个包含多个时间单位的对象。

返回值

  • 如果没有提供 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

Released under the MIT License.

本站访客数 人次 本站总访问量