外观
dayjs 日期格式化
约 4752 字大约 16 分钟
2024-01-01
介绍
提供功能全面的日期时间处理工具函数,基于 dayjs 库实现,支持日期的解析、格式化、比较、计算等操作,适用于各种日期处理场景。
解析日期
dayjs(input?: string | number | Date | Dayjs | null): Dayjs
创建或解析日期实例的主工厂函数。
参数
input: 可选,支持字符串日期、时间戳、Date 对象、Dayjs 对象或 null,默认为当前时间
返回值
<Dayjs>: Dayjs 实例,可进行链式调用
示例
import { dayjs } from '@/uni_modules/uview-unix';
// 获取当前时间
const current = dayjs();
console.log(current.format()); // 2024-01-01 12:00:00
// 解析字符串
const fromString = dayjs('2023-10-01');
console.log(fromString.format()); // 2023-10-01 00:00:00
// 解析时间戳(毫秒)
const fromTimestamp = dayjs(1696118400000);
console.log(fromTimestamp.format()); // 2023-10-01 00:00:00
// 解析 Date 对象
const fromDate = dayjs(new Date());
console.log(fromDate.format());
// 解析 Dayjs 对象
const fromDayjs = dayjs(current);
console.log(fromDayjs.format());unix(timestamp: number): Dayjs
从 Unix 时间戳(秒)创建 Dayjs 实例。
参数
timestamp: Unix 时间戳(秒)
返回值
<Dayjs>: Dayjs 实例
示例
import { dayjs, unix } from '@/uni_modules/uview-unix';
// 使用 unix 时间戳创建
const fromUnix = unix(1696118400);
console.log(fromUnix.format()); // 2023-10-01 00:00:00now(): Dayjs
获取当前时间的 Dayjs 实例,等同于 dayjs()。
返回值
<Dayjs>: 当前时间的 Dayjs 实例
示例
import { dayjs, now } from '@/uni_modules/uview-unix';
// 使用 now() 函数
const nowTime = now();
console.log(nowTime.format()); // 当前时间isDayjs(d: any): boolean
判断一个对象是否为 Dayjs 实例。
参数
d: 要判断的对象
返回值
<Boolean>: 是否为 Dayjs 实例
示例
import { dayjs, isDayjs } from '@/uni_modules/uview-unix';
// isDayjs
console.log(isDayjs(dayjs())); // true
console.log(isDayjs(new Date())); // false
console.log(isDayjs('string')); // false多语言设置
内置语言
'en': 英语'zh-CN': 中文(默认)
locale(language: string): void
设置全局语言环境,影响日期格式化中的星期、月份等显示。
参数
language: 语言代码,支持'en'(英语)、'zh-CN'(中文)
示例
import { dayjs, locale } from '@/uni_modules/uview-unix';
const current = dayjs('2024-01-01 12:00:00');
// 设置为中文
locale('zh-CN');
console.log(current.format('dddd')); // 星期一
console.log(current.format('MMMM')); // 一月
// 设置为英文
locale('en');
console.log(current.format('dddd')); // Monday
console.log(current.format('MMMM')); // January格式化日期
format(formatStr?: string): string
格式化日期为指定字符串格式。
参数
formatStr: 可选,格式化字符串,默认为'YYYY-MM-DD HH:mm:ss'
返回值
<String>: 格式化后的日期字符串
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
// 默认格式
console.log(current.format()); // 2024-01-01 12:00:00
// 自定义格式
console.log(current.format('YYYY-MM-DD')); // 2024-01-01
console.log(current.format('YYYY/MM/DD HH:mm:ss')); // 2024/01/01 12:00:00
console.log(current.format('YYYY年MM月DD日')); // 2024年01月01日
// 各种格式化标记
console.log(current.format('YYYY')); // 2024 (4位年)
console.log(current.format('YY')); // 24 (2位年)
console.log(current.format('MM')); // 01 (2位月)
console.log(current.format('M')); // 1 (月)
console.log(current.format('DD')); // 01 (2位日)
console.log(current.format('D')); // 1 (日)
console.log(current.format('HH')); // 12 (24小时)
console.log(current.format('H')); // 12 (24小时)
console.log(current.format('hh')); // 12 (12小时)
console.log(current.format('h')); // 12 (12小时)
console.log(current.format('mm')); // 00 (2位分)
console.log(current.format('m')); // 0 (分)
console.log(current.format('ss')); // 00 (2位秒)
console.log(current.format('s')); // 0 (秒)
console.log(current.format('SSS')); // 000 (毫秒)
console.log(current.format('A')); // AM
console.log(current.format('a')); // am
console.log(current.format('Z')); // +08:00
console.log(current.format('ZZ')); // +0800
console.log(current.format('dddd')); // Monday
console.log(current.format('ddd')); // Mon
console.log(current.format('dd')); // Mo支持的格式化字符
| 格式字符 | 描述 | 示例 |
|---|---|---|
YYYY | 四位年份 | 2024 |
YY | 两位年份 | 24 |
MM | 两位月份(01-12) | 05 |
M | 月份(1-12) | 5 |
DD | 两位日期(01-31) | 01 |
D | 日期(1-31) | 1 |
HH | 24 小时制两位小时(00-23) | 13 |
H | 24 小时制小时(0-23) | 3 |
hh | 12 小时制两位小时(01-12) | 01 |
h | 12 小时制小时(1-12) | 1 |
mm | 两位分钟(00-59) | 09 |
m | 分钟(0-59) | 9 |
ss | 两位秒数(00-59) | 05 |
s | 秒数(0-59) | 5 |
SSS | 三位毫秒数 | 789 |
A | 大写上午/下午(AM/PM) | AM |
a | 小写上午/下午(am/pm) | am |
Z | 时区(+08:00) | +08:00 |
ZZ | 短格式时区(+0800) | +0800 |
dddd | 完整星期名称 | Sunday |
ddd | 短格式星期名称 | Sun |
dd | 最小格式星期名称 | Su |
toISOString(): string
转换为 ISO 8601 格式字符串。
返回值
<String>: ISO 格式字符串
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
console.log(current.toISOString()); // 2024-01-01T12:00:00.000ZtoJSON(): string
转换为 JSON 格式(用于 JSON.stringify)。
返回值
<String>: JSON 字符串
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
console.log(current.toJSON()); // 2024-01-01T12:00:00.000Z
console.log(JSON.stringify({ date: current })); // {"date":"2024-01-01T12:00:00.000Z"}toString(): string
转换为字符串格式。
返回值
<String>: 字符串格式
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
console.log(current.toString()); // Mon Jan 01 2024 12:00:00 GMT+0800日期增减
add(amount: number, unit: UnitType): Dayjs
增加指定单位的时间。
参数
amount: 增加的数量unit: 时间单位,支持'year' | 'month' | 'date' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' | 'week' | 'quarter'
返回值
<Dayjs>: Dayjs 实例,支持链式调用
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
// 增加时间
console.log(current.clone().add(1, 'year').format()); // 2025-01-01 12:00:00
console.log(current.clone().add(2, 'month').format()); // 2024-03-01 12:00:00
console.log(current.clone().add(3, 'week').format()); // 2024-01-22 12:00:00
console.log(current.clone().add(7, 'day').format()); // 2024-01-08 12:00:00
console.log(current.clone().add(5, 'hour').format()); // 2024-01-01 17:00:00
console.log(current.clone().add(30, 'minute').format()); // 2024-01-01 12:30:00
console.log(current.clone().add(45, 'second').format()); // 2024-01-01 12:00:45
console.log(current.clone().add(500, 'millisecond').format()); // 2024-01-01 12:00:00subtract(amount: number, unit: UnitType): Dayjs
减少指定单位的时间。
参数
amount: 减少的数量unit: 时间单位,支持'year' | 'month' | 'date' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' | 'week' | 'quarter'
返回值
<Dayjs>: Dayjs 实例,支持链式调用
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
// 减少时间
console.log(current.clone().subtract(1, 'year').format()); // 2023-01-01 12:00:00
console.log(current.clone().subtract(2, 'month').format()); // 2023-11-01 12:00:00
console.log(current.clone().subtract(3, 'week').format()); // 2023-12-11 12:00:00
console.log(current.clone().subtract(7, 'day').format()); // 2023-12-25 12:00:00
console.log(current.clone().subtract(5, 'hour').format()); // 2024-01-01 07:00:00
console.log(current.clone().subtract(30, 'minute').format()); // 2024-01-01 11:30:00
console.log(current.clone().subtract(45, 'second').format()); // 2024-01-01 11:59:15
console.log(current.clone().subtract(500, 'millisecond').format()); // 2024-01-01 11:59:59获取/设置日期
get(unit: UnitType): number
获取指定单位的值。
参数
unit: 时间单位,支持'year' | 'month' | 'date' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' | 'week' | 'quarter'
返回值
<Number>: 对应单位的值(month 返回 1-12)
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs('2024-01-15 12:30:45');
// 使用 get() 方法
console.log(current.get('year')); // 2024
console.log(current.get('month')); // 1
console.log(current.get('date')); // 15
console.log(current.get('day')); // 1 (星期一)
console.log(current.get('hour')); // 12
console.log(current.get('minute')); // 30
console.log(current.get('second')); // 45
console.log(current.get('millisecond')); // 0
console.log(current.get('week')); // 3
console.log(current.get('quarter')); // 1set(unit: UnitType, value: number): Dayjs
设置指定单位的值。
参数
unit: 时间单位,支持'year' | 'month' | 'date' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'value: 要设置的值(month 需要传入 1-12)
返回值
<Dayjs>: Dayjs 实例,支持链式调用
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
// 使用 set() 方法
const setUsingMethod = current.clone()
.set('year', 2024)
.set('month', 6)
.set('date', 1);
console.log(setUsingMethod.format()); // 2024-07-01 12:00:00便捷方法
以下是常用的便捷方法,用于快速获取或设置日期的各个部分:
说明:这些方法支持
泛型参数,可以通过传入泛型来明确指定返回类型。当传入参数进行设置时,建议使用泛型<Dayjs>来确保返回 Dayjs 实例,从而支持链式调用。
| 方法 | 描述 | 参数 | 返回值 |
|---|---|---|---|
year(value?: number | null) | 获取/设置年份 | value: 可选,要设置的年份 | 获取时返回数字,设置时返回 Dayjs 实例 |
month(value?: number | null) | 获取/设置月份 | value: 可选,要设置的月份(0-11) | 获取时返回 1-12,设置时返回 Dayjs 实例 |
date(value?: number | null) | 获取/设置日期 | value: 可选,要设置的日期 | 获取时返回数字,设置时返回 Dayjs 实例 |
day(value?: number | null) | 获取/设置星期几(0-6,0 为周日) | value: 可选,要设置的星期几 | 获取时返回数字,设置时返回 Dayjs 实例 |
hour(value?: number | null) | 获取/设置小时 | value: 可选,要设置的小时 | 获取时返回数字,设置时返回 Dayjs 实例 |
minute(value?: number | null) | 获取/设置分钟 | value: 可选,要设置的分钟 | 获取时返回数字,设置时返回 Dayjs 实例 |
second(value?: number | null) | 获取/设置秒数 | value: 可选,要设置的秒数 | 获取时返回数字,设置时返回 Dayjs 实例 |
millisecond(value?: number | null) | 获取/设置毫秒 | value: 可选,要设置的毫秒 | 获取时返回数字,设置时返回 Dayjs 实例 |
quarter() | 获取季度(1-4) | 无 | 返回数字 |
示例
import { dayjs, type Dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
// 获取方法(不传参数,返回数字)
console.log(current.year<number>()); // 2024
console.log(current.month<number>()); // 1
console.log(current.date()); // 1
console.log(current.day()); // 1 (星期一)
console.log(current.hour()); // 12
console.log(current.minute()); // 0
console.log(current.second()); // 0
console.log(current.millisecond()); // 0
console.log(current.quarter()); // 1
// 设置方法(传入参数,返回 Dayjs 实例,支持链式调用)
// 使用泛型 <Dayjs> 明确指定返回类型
console.log(current.clone().year<Dayjs>(2025).format()); // 2025-01-01 12:00:00
console.log(current.clone().month<Dayjs>(5).format()); // 2024-06-01 12:00:00
console.log(current.clone().date<Dayjs>(15).format()); // 2024-01-15 12:00:00
console.log(current.clone().day<Dayjs>(1).format()); // 2024-01-01 12:00:00
console.log(current.clone().hour<Dayjs>(10).format()); // 2024-01-01 10:00:00
console.log(current.clone().minute<Dayjs>(30).format()); // 2024-01-01 12:30:00
console.log(current.clone().second<Dayjs>(45).format()); // 2024-01-01 12:00:45
console.log(current.clone().millisecond<Dayjs>(123).format()); // 2024-01-01 12:00:00
// 链式调用示例(使用泛型确保类型正确)
const chainDate = dayjs()
.year<Dayjs>(2024)
.month<Dayjs>(6)
.date<Dayjs>(15)
.hour<Dayjs>(10)
.minute<Dayjs>(30)
.second<Dayjs>(45)
.format('YYYY-MM-DD HH:mm:ss');
console.log(chainDate); // 2024-06-15 10:30:45日期范围
startOf(unit: UnitType): Dayjs
设置为指定单位的开始时间。
参数
unit: 时间单位,支持'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'
返回值
<Dayjs>: Dayjs 实例,支持链式调用
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs('2024-01-15 12:30:45');
// 起始时间
console.log(current.clone().startOf('year').format()); // 2024-01-01 00:00:00
console.log(current.clone().startOf('quarter').format()); // 2024-01-01 00:00:00
console.log(current.clone().startOf('month').format()); // 2024-01-01 00:00:00
console.log(current.clone().startOf('week').format()); // 2024-01-08 00:00:00
console.log(current.clone().startOf('day').format()); // 2024-01-15 00:00:00
console.log(current.clone().startOf('hour').format()); // 2024-01-15 12:00:00
console.log(current.clone().startOf('minute').format()); // 2024-01-15 12:30:00
console.log(current.clone().startOf('second').format()); // 2024-01-15 12:30:45endOf(unit: UnitType): Dayjs
设置为指定单位的结束时间。
参数
unit: 时间单位,支持'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'
返回值
<Dayjs>: Dayjs 实例,支持链式调用
示例
const current = dayjs('2024-01-15 12:30:45');
// 结束时间
console.log(current.clone().endOf('year').format()); // 2024-12-31 23:59:59
console.log(current.clone().endOf('quarter').format()); // 2024-03-31 23:59:59
console.log(current.clone().endOf('month').format()); // 2024-01-31 23:59:59
console.log(current.clone().endOf('week').format()); // 2024-01-14 23:59:59
console.log(current.clone().endOf('day').format()); // 2024-01-15 23:59:59
console.log(current.clone().endOf('hour').format()); // 2024-01-15 12:59:59
console.log(current.clone().endOf('minute').format()); // 2024-01-15 12:30:59
console.log(current.clone().endOf('second').format()); // 2024-01-15 12:30:45日期比较
diff(date: Dayjs, unit?: UnitType, floating?: boolean): number
计算与指定日期的差值。
参数
date: 要比较的 Dayjs 实例unit: 可选,差值单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'floating: 可选,是否返回浮点数,默认为false
返回值
<Number>: 时间差值
示例
import { dayjs } from '@/uni_modules/uview-unix';
const diffDate1 = dayjs('2023-01-01');
const diffDate2 = dayjs('2023-02-15');
// diff - 不同单位
console.log(diffDate2.diff(diffDate1, 'year')); // 0
console.log(diffDate2.diff(diffDate1, 'month')); // 1
console.log(diffDate2.diff(diffDate1, 'quarter')); // 0
console.log(diffDate2.diff(diffDate1, 'week')); // 6
console.log(diffDate2.diff(diffDate1, 'day')); // 45
console.log(diffDate2.diff(diffDate1, 'hour')); // 1080
console.log(diffDate2.diff(diffDate1, 'minute')); // 64800
console.log(diffDate2.diff(diffDate1, 'second')); // 3888000
console.log(diffDate2.diff(diffDate1, 'millisecond')); // 3888000000
// diff - 浮点数
console.log(diffDate2.diff(diffDate1, 'day', true)); // 45
console.log(diffDate2.diff(diffDate1, 'month', true)); // 1.4516129032258065isAfter(date: string | number | Date | Dayjs, unit?: UnitType): boolean
判断是否在指定日期之后。
参数
date: 要比较的日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'
返回值
<Boolean>: 是否在指定日期之后
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
// isAfter
console.log(date1.isAfter(date2)); // false
console.log(date2.isAfter(date1)); // true
console.log(date1.isAfter(date2, 'day')); // false
console.log(date2.isAfter(date1, 'day')); // trueisBefore(date: string | number | Date | Dayjs, unit?: UnitType): boolean
判断是否在指定日期之前。
参数
date: 要比较的日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'
返回值
<Boolean>: 是否在指定日期之前
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
// isBefore
console.log(date1.isBefore(date2)); // true
console.log(date2.isBefore(date1)); // false
console.log(date1.isBefore(date2, 'day')); // true
console.log(date2.isBefore(date1, 'day')); // falseisSame(date: string | number | Date | Dayjs, unit?: UnitType): boolean
判断是否与指定日期相同。
参数
date: 要比较的日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'
返回值
<Boolean>: 是否与指定日期相同
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
const date3 = dayjs('2023-01-01');
// isSame
console.log(date1.isSame(date3)); // true
console.log(date1.isSame(date2)); // false
console.log(date1.isSame(date3, 'day')); // true
console.log(date1.isSame(date2, 'day')); // falseisSameOrBefore(date: string | number | Date | Dayjs, unit?: UnitType): boolean
判断是否与指定日期相同或在其之前。
参数
date: 要比较的日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'
返回值
<Boolean>: 判断结果
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
const date3 = dayjs('2023-01-01');
// isSameOrBefore
console.log(date1.isSameOrBefore(date3)); // true
console.log(date1.isSameOrBefore(date2)); // true
console.log(date2.isSameOrBefore(date1)); // falseisSameOrAfter(date: string | number | Date | Dayjs, unit?: UnitType): boolean
判断是否与指定日期相同或在其之后。
参数
date: 要比较的日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'
返回值
<Boolean>: 判断结果
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
const date3 = dayjs('2023-01-01');
// isSameOrAfter
console.log(date1.isSameOrAfter(date3)); // true
console.log(date2.isSameOrAfter(date1)); // true
console.log(date1.isSameOrAfter(date2)); // falseisBetween(date1: string | number | Date | Dayjs, date2: string | number | Date | Dayjs, unit?: UnitType, inclusivity?: '()' | '[]' | '(]' | '[)'): boolean
判断是否在两个日期之间。
参数
date1: 起始日期,支持字符串、时间戳、Date 对象或 Dayjs 实例date2: 结束日期,支持字符串、时间戳、Date 对象或 Dayjs 实例unit: 可选,比较单位,支持'year' | 'month' | 'week' | 'date' | 'day' | 'hour' | 'quarter' | 'minute' | 'second' | 'millisecond',默认为'millisecond'inclusivity: 可选,包含范围,默认'()'(不包含边界),支持'()'(不包含边界)、'[]'(包含边界)、'(]'(不包含起始,包含结束)、'[)'(包含起始,不包含结束)
返回值
<Boolean>: 判断结果
示例
import { dayjs } from '@/uni_modules/uview-unix';
const date1 = dayjs('2023-01-01');
const date2 = dayjs('2023-01-02');
const dateBetween = dayjs('2023-01-15');
// isBetween
console.log(dateBetween.isBetween(date1, date2, 'day')); // false
console.log(dateBetween.isBetween(date1, date2, 'day', '[]')); // false
console.log(dateBetween.isBetween(date1, date2, 'day', '()')); // false
console.log(dateBetween.isBetween(date1, date2, 'day', '[)')); // false
console.log(dateBetween.isBetween(date1, date2, 'day', '(]')); // false
const dateBetween2 = dayjs('2023-01-01');
console.log(dateBetween2.isBetween(date1, date2, 'day', '[]')); // true
console.log(dateBetween2.isBetween(date1, date2, 'day', '[)')); // true
console.log(dateBetween2.isBetween(date1, date2, 'day', '()')); // falseisToday(): boolean
判断是否是今天。
返回值
<Boolean>: 是否是今天
示例
import { dayjs } from '@/uni_modules/uview-unix';
const today = dayjs();
const yesterday = dayjs().subtract(1, 'day');
const tomorrow = dayjs().add(1, 'day');
console.log(today.isToday()); // true
console.log(yesterday.isToday()); // false
console.log(tomorrow.isToday()); // falseisYesterday(): boolean
判断是否是昨天。
返回值
<Boolean>: 是否是昨天
示例
import { dayjs } from '@/uni_modules/uview-unix';
const today = dayjs();
const yesterday = dayjs().subtract(1, 'day');
const tomorrow = dayjs().add(1, 'day');
console.log(yesterday.isYesterday()); // true
console.log(today.isYesterday()); // false
console.log(tomorrow.isYesterday()); // falseisTomorrow(): boolean
判断是否是明天。
返回值
<Boolean>: 是否是明天
示例
import { dayjs } from '@/uni_modules/uview-unix';
const today = dayjs();
const yesterday = dayjs().subtract(1, 'day');
const tomorrow = dayjs().add(1, 'day');
console.log(tomorrow.isTomorrow()); // true
console.log(today.isTomorrow()); // false
console.log(yesterday.isTomorrow()); // false日期转换
valueOf(): number
获取日期的时间戳(毫秒)。
返回值
<Number>: 时间戳
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs('2024-01-01 12:00:00');
console.log(current.valueOf()); // 1704096000000unix(): number
获取 Unix 时间戳(秒)。
返回值
<Number>: Unix 时间戳
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs('2024-01-01 12:00:00');
console.log(current.unix()); // 1704096000toDate(): Date
转换为 JavaScript 原生 Date 对象。
返回值
<Date>: Date 实例
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
console.log(current.toDate()); // Mon Jan 01 2024 12:00:00 GMT+0800实用方法
clone(): Dayjs
克隆当前 Dayjs 实例。
返回值
<Dayjs>: 新的 Dayjs 实例
示例
import { dayjs } from '@/uni_modules/uview-unix';
const original = dayjs('2023-01-01');
console.log(original.format()); // 2023-01-01 00:00:00
const cloned = original.clone();
console.log(cloned.format()); // 2023-01-01 00:00:00
console.log(cloned.isSame(original)); // true
const modified = original.add(1, 'day');
console.log(modified.format()); // 2023-01-02 00:00:00
console.log(original.format()); // 2023-01-01 00:00:00 (未改变)isValid(): boolean
验证日期是否有效。
返回值
<Boolean>: 日期是否有效
示例
import { dayjs } from '@/uni_modules/uview-unix';
const validDate = dayjs('2023-01-01');
const invalidDate = dayjs('invalid');
console.log(validDate.isValid()); // true
console.log(invalidDate.isValid()); // falseutcOffset(): number
获取时区偏移量(分钟)。
返回值
<Number>: 时区偏移量
示例
import { dayjs } from '@/uni_modules/uview-unix';
const current = dayjs();
console.log(current.utcOffset()); // -480 (东八区)daysInMonth(): number
获取当前月份的天数。
返回值
<Number>: 当月天数
示例
import { dayjs } from '@/uni_modules/uview-unix';
const january = dayjs('2023-01-01');
const february = dayjs('2023-02-01');
const februaryLeap = dayjs('2024-02-01');
console.log(january.daysInMonth()); // 31
console.log(february.daysInMonth()); // 28
console.log(februaryLeap.daysInMonth()); // 29实际应用场景
格式化日期显示
import { dayjs, type Dayjs } from '@/uni_modules/uview-unix';
const formatDate = (date: Dayjs): string => {
return date.format('YYYY年MM月DD日');
};
console.log(formatDate(dayjs())); // 2024年01月01日计算年龄
const birthDate = dayjs('1990-01-01');
const age = dayjs().diff(birthDate, 'year');
console.log(age); // 34判断是否过期
import { dayjs } from '@/uni_modules/uview-unix';
const expiryDate = dayjs('2023-12-31');
const isExpired = dayjs().isAfter(expiryDate);
console.log(isExpired); // true计算倒计时
const targetDate = dayjs().add(7, 'day');
const daysLeft = targetDate.diff(dayjs(), 'day');
console.log(daysLeft); // 7获取本周的开始和结束
import { dayjs } from '@/uni_modules/uview-unix';
const weekStart = dayjs().startOf('week').format('YYYY-MM-DD');
const weekEnd = dayjs().endOf('week').format('YYYY-MM-DD');
console.log('本周开始: ' + weekStart);
console.log('本周结束: ' + weekEnd);获取本月的开始和结束
const monthStart = dayjs().startOf('month').format('YYYY-MM-DD');
const monthEnd = dayjs().endOf('month').format('YYYY-MM-DD');
console.log('本月开始: ' + monthStart);
console.log('本月结束: ' + monthEnd);获取本季度的开始和结束
import { dayjs } from '@/uni_modules/uview-unix';
const quarterStart = dayjs().startOf('quarter').format('YYYY-MM-DD');
const quarterEnd = dayjs().endOf('quarter').format('YYYY-MM-DD');
console.log('本季度开始: ' + quarterStart);
console.log('本季度结束: ' + quarterEnd);相对时间
import { dayjs, type Dayjs } from '@/uni_modules/uview-unix';
const relativeTime = (date: Dayjs): string => {
const now = dayjs();
const diff = now.diff(date, 'day');
if (diff == 0) return '今天';
if (diff == 1) return '昨天';
if (diff == -1) return '明天';
if (diff > 1) return `${diff}天前`;
if (diff < -1) return `${Math.abs(diff)}天后`;
return date.format('YYYY-MM-DD');
};
console.log(relativeTime(dayjs())); // 今天
console.log(relativeTime(dayjs().subtract(1, 'day'))); // 昨天
console.log(relativeTime(dayjs().add(1, 'day'))); // 明天
console.log(relativeTime(dayjs().subtract(7, 'day'))); // 7天前
console.log(relativeTime(dayjs().add(7, 'day'))); // 7天后链式调用
import { dayjs, type Dayjs } from '@/uni_modules/uview-unix';
// 复杂的链式调用
const chainResult = dayjs()
.year<Dayjs>(2023)
.month<Dayjs>(5)
.date<Dayjs>(15)
.hour<Dayjs>(10)
.minute<Dayjs>(30)
.second<Dayjs>(45)
.format('YYYY-MM-DD HH:mm:ss');
console.log(chainResult); // 2023-06-15 10:30:45
// 计算下个月的第一个星期一
const nextMonthFirstMonday = dayjs()
.add(1, 'month')
.startOf('month')
.day<Dayjs>(1)
.format('YYYY-MM-DD');
console.log(nextMonthFirstMonday); // 2024-02-05
// 计算本月的最后一天
const lastDayOfMonth = dayjs().endOf('month').format('YYYY-MM-DD');
console.log(lastDayOfMonth); // 2024-01-31
// 计算两个日期之间的工作日(简单示例)
const startDate = dayjs('2023-01-01');
const endDate = dayjs('2023-01-31');
const daysDiff = endDate.diff(startDate, 'day');
console.log(daysDiff); // 30