You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用DayJs获取当前日期前15天的日、月、年对应数值

Dayjs分字段获取指定偏移日期的实现方法

你可以先将偏移后的日期预存为一个Dayjs实例,再基于这个实例分别格式化取出日、月、年的值即可,subtract方法会自动处理跨月、跨年、大小月、闰年等边缘场景,不需要手动处理日期计算逻辑:

// 计算得到当前日期往前推15天的目标日期实例
const targetDate = Cypress.dayjs().subtract(15, 'day')

// 分别对三个输入框做值校验
cy.get('input[name="day"]').should('have.value', targetDate.format('DD'))
cy.get('input[name="month"]').should('have.value', targetDate.format('MM'))
cy.get('input[name="year"]').should('have.value', targetDate.format('YY'))

内容的提问来源于stack exchange,提问作者ZombiePie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 17:45:04