Appearance
时间操作
一个轻量的处理时间和日期的 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日
支持的格式化占位符列表
占位符 | 输出 | 说明 |
---|---|---|
YY | 23 | 两位数的年份 |
YYYY | 2023 | 四位数的年份 |
M | 1-12 | 月份,从 1 开始 |
MM | 01-12 | 月份,两位数 |
D | 1-31 | 月份里的一天 |
DD | 01-12 | 月份里的一天,两位数 |
H | 0-23 | 小时 |
HH | 00-23 | 小时,两位数 |
m | 0-59 | 分钟 |
mm | 00-59 | 分钟,两位数 |
s | 0-59 | 秒 |
ss | 00-59 | 秒,两位数 |
SSS | 000-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 分钟
参数
参数 | 类型 | 默认值 | 是否必选 | 说明 |
---|---|---|---|---|
startDate | Date、Number | new Date() | ✔ | 开始的时间 |
endDate | Date、Number | — | ✘ | 结束的时间 |
options | Object | — | ✘ | 扩展选项 |
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分
参数
参数 | 类型 | 默认值 | 是否必选 | 说明 |
---|---|---|---|---|
duration | Number | — | ✔ | 多少毫秒 |
options | Object | — | ✘ | 扩展选项 |
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
}
)
参数
参数 | 类型 | 默认值 | 是否必选 | 说明 |
---|---|---|---|---|
range | Array<Date、Number> | — | ✔ | 需要比较的时间范围 |
date | Date、Number | — | ✔ | 需要比较的时间 |
options | Object | — | ✘ | 扩展配置 |
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 参数
参数 | 类型 | 默认值 | 是否必选 | 说明 |
---|---|---|---|---|
date | Date、Number | — | ✔ | 指定的比较时间 |
unit | String | millisecond | ✘ | 需要比较的单位 |