Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
How to get current date using cy.clock()

How can i get the date in format dd/mm/yyyy using cy.clock() and put the date in a textfield.. I have seen most examples are getting the timestamp but i dont need the time. Just need the current day date.

I do not want to use custom command here. Thank you

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The cy.clock() is about controlling the perceived date of the app.

From the examples,

Let the test run as at a certain date

const now = new Date(2017, 3, 14).getTime() // April 14, 2017 timestamp

cy.clock(now)
cy.visit('/index.html')
cy.get('#date').contains('2017-04-14')  

Typing into a field with a certain format

If you want to .type() a particular date string, convert with .toLocaleDateString()

const d = new Date()  // current date
// or
const d = new Date(2017, 3, 14)  // specific date

cy.get('input').type(d.toLocaleDateString('en-GB'))  // type in as 'dd/mm/yyyy'

Combining the two, e.g testing validation

// Set clock to a specific date
const now = new Date(2017, 3, 14).getTime() // April 14, 2017 timestamp
cy.clock(now)
cy.visit('/index.html')

// Type in an earlier date
const d = new Date(2017, 3, 13)
cy.get('input').type(d.toLocaleDateString('en-GB'))
  .blur()    // fire validation
  .should('contain', 'Error: Date entered must be a future date')
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use day.js to get the current date and format it accordingly.

const dayjs = require('dayjs')

//In test
cy.log(dayjs().format('DD/MM/YYYY'))  //Prints todays date 30/09/2021
cy.get('textfield').type(dayjs().format('DD/MM/YYYY')) //input today's date in DD/MM/YYYY format
about 4 years ago · Juan Pablo Isaza Denunciar

0

I stumbled of this thread because I was searching for a way to retrieve the "current date" in the test. So, for other people who are searching for this: in another thread I found the answer:

cy.window().then((win) => {
  const newDate = new Date(win.Date());
  // do something usefull with the date
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda