添加時間
返回一個添加了指定時間量的 Day.js 物件的新實例(複製)。
js
const a = dayjs();
const b = a.add(7, 'day');
// a -> 原始 Day.js 物件,保持不變
// b -> 添加時間後的新 Day.js 物件
時間單位不區分大小寫,支援複數和簡寫形式。請注意,簡寫形式區分大小寫。
所有可用單位列表
Unit | Shorthand | Description |
---|---|---|
day | d | 天 |
week | w | 星期 |
month | M | 月 |
quarter | Q | 季度 (需要安裝 QuarterOfYear 插件) |
year | y | 年 |
hour | h | 小時 |
minute | m | 分鐘 |
second | s | 秒 |
millisecond | ms | 毫秒 |
或者,您可以使用 durations 來為 Day.js 物件增加時間間隔。
js
result = dayjs().add(dayjs.duration({ days: 1 }));
當為 days (天) 和 weeks (星期) 傳遞小數數值時,它們會在添加之前被四捨五入到最接近的整數。