Skip to content

时间操作

一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样。

安装

sh
# npm
npm install @neosjs/utils --save-dev
# yarn
yarn add @neosjs/utils
# pnpm
pnpm add @neosjs/utils
# npm
npm install @neosjs/utils --save-dev
# yarn
yarn add @neosjs/utils
# pnpm
pnpm add @neosjs/utils

formatDate

根据传入的占位符返回格式化后的日期。

ts
import { formatDate } from '@neosjs/utils'

formatDate(+new Date())
formatDate(+new Date(), 'YYYY-MM-DD HH:mm:ss')
import { formatDate } from '@neosjs/utils'

formatDate(+new Date())
formatDate(+new Date(), 'YYYY-MM-DD HH:mm:ss')

示例

2023-05-032023年05月03日

支持的格式化占位符列表

占位符输出说明
YY23两位数的年份
YYYY2023四位数的年份
M1-12月份,从 1 开始
MM01-12月份,两位数
D1-31月份里的一天
DD01-12月份里的一天,两位数
H0-23小时
HH00-23小时,两位数
m0-59分钟
mm00-59分钟,两位数
s0-59
ss00-59秒,两位数
SSS000-999毫秒,三位数

formatDistance

获取指定时间距离当前时间或者指定时间多远

ts
import { formatDistance } from '@neosjs/utils'

formatDistance(new Date('2023-01-01'), new Date('2023-01-03')) // 2 天内
formatDistance(1658717927, 1658320372, { locale: 'en' }) // 7 minutes ago
formatDistance(1658320372, 1658717927, { locale: 'en' }) // in 7 minutes
formatDistance(1658320372, 1658717927, { locale: 'zh-cn', withoutSuffix: true }) // 7 分钟
import { formatDistance } from '@neosjs/utils'

formatDistance(new Date('2023-01-01'), new Date('2023-01-03')) // 2 天内
formatDistance(1658717927, 1658320372, { locale: 'en' }) // 7 minutes ago
formatDistance(1658320372, 1658717927, { locale: 'en' }) // in 7 minutes
formatDistance(1658320372, 1658717927, { locale: 'zh-cn', withoutSuffix: true }) // 7 分钟

示例

2 天内7 分钟前in 7 minutes7 分钟

参数

参数类型默认值是否必选说明
startDateDate、Numbernew Date()开始的时间
endDateDate、Number结束的时间
optionsObject扩展选项

formatDuration

将指定秒转为‘H小时M分钟S秒’,H、M、S为0时,默认不展示;如果想更改格式可传入第二个扩展参数options

ts
import { formatDuration } from '@neosjs/utils'

formatDuration(60000, 'm') // 1分钟11秒
formatDuration(71, { lang: 'en' }) // 1分钟11秒
formatDuration(80220, { zero: true }) // 22小时17分钟0秒
formatDuration(80221, { lang: 'en', delimiter: ',' }) // 22小时,17分钟,1秒
import { formatDuration } from '@neosjs/utils'

formatDuration(60000, 'm') // 1分钟11秒
formatDuration(71, { lang: 'en' }) // 1分钟11秒
formatDuration(80220, { zero: true }) // 22小时17分钟0秒
formatDuration(80221, { lang: 'en', delimiter: ',' }) // 22小时,17分钟,1秒

示例

0000-0005小时33分

参数

参数类型默认值是否必选说明
durationNumber多少毫秒
optionsObject扩展选项

isBetweenDate

isBetweenDate 返回一个 boolean 来展示一个时间是否介于两个时间之间.

ts
import { isBetweenDate } from '@neosjs/utils'

const between = isBetweenDate(
  ['2023-01-01', '2023-10-30'],
  '2023-01-03',
  {
    unit: 'day',
    start: false
  }
)
import { isBetweenDate } from '@neosjs/utils'

const between = isBetweenDate(
  ['2023-01-01', '2023-10-30'],
  '2023-01-03',
  {
    unit: 'day',
    start: false
  }
)

参数

参数类型默认值是否必选说明
rangeArray<Date、Number>需要比较的时间范围
dateDate、Number需要比较的时间
optionsObject扩展配置

isSameDate

isSameDate 返回一个 Boolean 来展示当前日期是否和提供的日期时间相同.

ts
import { isSameDate } from '@neosjs/utils'

const isSame = isSameDate('2023-01-03') // retuen false
import { isSameDate } from '@neosjs/utils'

const isSame = isSameDate('2023-01-03') // retuen false

isBeforeDate

isBeforeDate 返回一个 Boolean 来展示当前日期是否在提供的日期时间之前.

ts
import { isBeforeDate } from '@neosjs/utils'

const isBefore = isBeforeDate('2023-01-03') // return true
import { isBeforeDate } from '@neosjs/utils'

const isBefore = isBeforeDate('2023-01-03') // return true

isAfterDate

isAfterDate 返回一个 Boolean 来展示当前日期是否在提供的日期时间之后.

ts
import { isAfterDate } from '@neosjs/utils'

const isAfter = isAfterDate('2023-01-03') // return false
import { isAfterDate } from '@neosjs/utils'

const isAfter = isAfterDate('2023-01-03') // return false

isSameDate、isBeforeDate、isAfterDate 参数

参数类型默认值是否必选说明
dateDate、Number指定的比较时间
unitStringmillisecond需要比较的单位

Released under the MIT License.

时间操作 has loaded