如何使用DayJs获取当前日期之前的日期。
我知道如何获取当前日期,但我们可以从当前日期中删除15天吗?
cy.get('input[name="day"]').should('have.value', (Cypress.dayjs().format('DD')))
cy.get('input[name="month"]').should('have.value', (Cypress.dayjs().format('MM')))
cy.get('input[name="year"]').should('have.value', (Cypress.dayjs().format('YY'))) ```
This is my code for getting the currentDate. I would like to get 15 days before my current Date. I would have used substract if the date was one input field but here we have different placeholders for the inputs.发布于 2021-10-27 10:17:25
对于可读性测试,只需计算一次目标。
const target = dayjs().subtract(15, 'day')
cy.get('input[name="day"]').should('have.value', target.format('DD'))
cy.get('input[name="month"]').should('have.value', target.format('MM'))
cy.get('input[name="year"]').should('have.value', target .format('YY'))发布于 2021-10-26 10:56:13
发布于 2021-10-26 11:06:38
正如@pavelsaman提到的那样,.subtract将会工作。您只需将其格式化为提取日、月、年。
const dayjs = require('dayjs')
cy.get('input[name="day"]').should(
'have.value',
dayjs().subtract(15, 'day').format('DD')
) //11
cy.get('input[name="month"]').should(
'have.value',
dayjs().subtract(15, 'day').format('MM')
) //10
cy.get('input[name="year"]').should(
'have.value',
dayjs().subtract(15, 'day').format('YY')
) //21https://stackoverflow.com/questions/69721679
复制相似问题