¿Cómo obtenemos una fecha anterior a la fecha actual usando DayJs?
Sé cómo obtener la fecha actual, pero ¿podemos eliminar 15 días de la fecha actual?
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.De la documentación :
dayjs().subtract(15, 'day'); Las unidades disponibles se mencionan aquí , day es una de ellas.
Como mencionó .subtract , .subtract funcionará. Solo tienes que formatearlo para extraer día, mes, año.
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') ) //21Para una prueba legible, calcule el objetivo una vez.
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'))