Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

384
Views
Compare two dates in cypress

How can I compare two dates in Cypress without them beeing modified?

             let today = new Date();
let dateDisplayed = "22.07.2022".split('.');
    let dateToCompare = new Date(
           +dateDisplayed[2],
           +dateDisplayed[1] - 1,
           +dateDisplayed[0]
            );
    let firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
    expect(dateToCompare).greaterThan(firstDayOfMonth);

When I run the tests I get the following output.

Cypress log test file

Does the 'expect' function modify my date values? It seems that both values are decreased by one day. How do I overcome this problem?

Thanks for your help!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

They aren't modified as much as translated from your time zone (Central European Summer Time, is it?) to GMT. Your midnight is 10pm in Greenwich.

Considering that it happened to both dates, it doesn't change the result of the comparison.

If it nevertheless bothers you, there's Cypress' clock function to influence how it handles, among other things, Date objects.

https://docs.cypress.io/api/commands/clock

about 4 years ago · Santiago Gelvez Report

0

You can use day.js library for this. For assertion, you can apply the isAfter to compare both the dates like below:

const dayjs = require('dayjs')

describe('test suite', () => {
  it('Test', () => {
    let dateDisplayed = '22.07.2022'.split('.')
    let dateToCompare =
      dateDisplayed[2] +
      '-' +
      (+dateDisplayed[1] - 1).toString() +
      '-' +
      dateDisplayed[0]
    let firstDayOfMonth = dayjs().startOf('month') //2022-07-01T00:00:00+03:00
    const result = firstDayOfMonth.isAfter(dayjs(dateToCompare))
    cy.then(() => {
      expect(result).to.eq(true)
    })
  })
})
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!