Skip to content

取值/赋值

毫秒

获取或设置毫秒。
传入0到999的数字,如果超出这个范围,它会进位到秒。

ts
dateuts().millisecond()
dateuts().millisecond(1)

获取或设置秒。
传入0到59的数字,如果超出这个范围,它会进位到分钟。

ts
dateuts().second()
dateuts().second(1)

分钟

获取或设置分钟。
传入0到59的数字,如果超出这个范围,它会进位到小时。

ts
dateuts().minute()
dateuts().minute(1)

小时

获取或设置小时。
传入0到23的数字,如果超出这个范围,它会进位到天数。

ts
dateuts().hour()
dateuts().hour(1)

日期

获取或设置月份中的日期。
传入1到31的数字,如果超出这个范围,它会进位到月份。

ts
dateuts().date()
dateuts().date(1)

星期

获取或设置星期。
传入0(星期天)到6(星期一)的数字,如果超出这个范围,它会进位到其他周。

ts
dateuts().day()
dateuts().day(1)

季度

获取或设置季度。

ts
dateuts().quarter()
dateuts().quarter(1)

月份

获取或设置月份。
传入0(1月)到11(12月)的数字,如果超出这个范围,它会进位到年。

ts
dateuts().month()
dateuts().month(1)

注意

获取的月份是从0开始的,1月返回的是0,2月返回的是1,以此类推;
设置的月份也是从0开始的,设置为1月应传入0,2月传入1,以此类推;

获取或设置年。

ts
dateuts().year()
dateuts().year(1)

Get

从 dateuts 对象中获取相应信息的 getter。参数为日期单位
相对于:

ts
//unit 就是 year 这些单位
dateuts()[unit]()
ts
dateuts().get('year') //年
dateuts().get('month') //月
dateuts().get('date') //日
dateuts().get('day') //星期
dateuts().get('quarter') //季度
dateuts().get('hour') //小时
dateuts().get('minute') //分钟
dateuts().get('second') //秒
dateuts().get('millisecond') //毫秒

Set

通用的 setter,两个参数分别是要更新的单位和数值,调用后会返回一个修改后的新实例。
相对于:

ts
//unit 就是 year 这些单位 value就是数值
dateuts()[unit](value)
ts
dateuts().set('year',1)
dateuts().set('month',1) //设置为2月,月份从0开始的
dateuts().set('date',1)
dateuts().set('day',1)
dateuts().set('quarter',1)
dateuts().set('hour',1)
dateuts().set('minute',1)
dateuts().set('second',1)
dateuts().set('millisecond',1)

支持链式调用

ts
dateuts().set('year',2026).set('month',2).set('date',3).format('YYYY-MM-DD')
//2026-03-03

Unix时间戳(毫秒)

返回当前实例的 UNIX 时间戳,13位数字,毫秒

ts
dateuts().valueOf()

Unix时间戳(秒)

返回当前实例的 UNIX 时间戳,10位数字,秒。

ts
dateuts().unix()

获取月天数

获取当前月份包含的天数。

ts
dateuts().daysInMonth()
dateuts('2025-02').daysInMonth() //28

转数组

返回一个包含各个时间信息的 Array 。

ts
dateuts('2025-03-15 16:00').toArray()
//[2025, 2, 15, 16, 0, 0, 0]

转对象

返回包含时间信息的 Object。

ts
dateuts('2025-03-15 16:00').toObject()
/**
 * {
    "year": 2025,
    "month": 2,
    "date": 15,
    "hours": 16,
    "minutes": 0,
    "seconds": 0,
    "milliseconds": 0
}
 */

转Date

返回dateuts中获取原生的 Date 对象

ts
dateuts('2025-03-15 16:00').toDate()