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

287
Views
Cypress: JavaScript Date format string gets converted to NaN

In one of my automation tests, I am importing a date format stored in the environment parameters, use it for my test and then increment it by one second after the test is executed. This is the code:

afterEach(()=>{
    var startTimeStamp = Cypress.env('aestartTimestamp');
    var startTimestampString = String(startTimeStamp);
    var epochStartTimeStamp = new Date(startTimestampString);
    var startYear = epochStartTimeStamp.getFullYear();
    var month = (epochStartTimeStamp.getMonth())+1;
    var realStartMonth = month <=9 ? '0'+month:month;
    var startDate = epochStartTimeStamp.getDate()<=9 ? '0'+epochStartTimeStamp.getDate():epochStartTimeStamp.getDate();
    var startHours = epochStartTimeStamp.getHours() <=9 ? '0'+epochStartTimeStamp.getHours():epochStartTimeStamp.getHours();
    var startMinutes = epochStartTimeStamp.getMinutes() <=9 ? '0'+epochStartTimeStamp.getMinutes():epochStartTimeStamp.getMinutes(); 
    var startSeconds = (epochStartTimeStamp.getSeconds()+1)<=9? '0'+(epochStartTimeStamp.getSeconds()+1):(epochStartTimeStamp.getSeconds()+1);
    var newStartTimeStamp = startYear+"-"+realStartMonth+"-"+startDate+" "+startHours+":"+startMinutes+":"+startSeconds;
    Cypress.env('aestartTimestamp',newStartTimeStamp);
    console.log(newStartTimeStamp);

    var endTimeStamp = Cypress.env('aeendTimestamp');
    var endTimeStampString = String(endTimeStamp);
    var epochEndTimeStamp = new Date(endTimeStampString);
    var endYear = epochEndTimeStamp.getFullYear();
    var endMonth = (epochEndTimeStamp.getMonth())+1;
    var realEndMonth = endMonth <=9 ? '0'+endMonth:endMonth;
    var endDate = epochEndTimeStamp.getDate()<=9 ? '0'+epochEndTimeStamp.getDate():epochEndTimeStamp.getDate();
    var endHours = epochEndTimeStamp.getHours() <=9 ? '0'+epochEndTimeStamp.getHours():epochEndTimeStamp.getHours();
    var endMinutes = epochEndTimeStamp.getMinutes() <=9 ? '0'+epochEndTimeStamp.getMinutes():epochEndTimeStamp.getMinutes();
    var endSeconds = (epochEndTimeStamp.getSeconds()+1) <=9 ? '0'+(epochEndTimeStamp.getSeconds()+1):(epochEndTimeStamp.getSeconds()+1);
    var newEndTimestamp = endYear+"-"+realEndMonth+"-"+endDate+" "+endHours+":"+endMinutes+":"+endSeconds;
    Cypress.env('aeendTimeStamp',newEndTimestamp);
    console.log(newEndTimestamp);
})

However, after running the tests, the date gets converted to NaN. This is what I see in the console:

enter image description here

The timestamp is converted to NaN.

When I run the code independently in any browser console, I see that the code works just perfectly. (The new time stamp is incremented by 1 second).

So I am mystified as to why the code works properly in the dev console but not in the Cypress. Test. Is there something missing?

about 4 years ago · Juan Pablo Isaza
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!