Issue occurs when I need to check for example that 11AM was 2hours before 1PM as my code takes difference as 10.(11 vs 1) as we have 12 hour time.
const lines2 = text.split('n'); const lines8 = text.split(' '); const lines9 = text.split(':', 1); // here is where I split it only to hour. //same for second time const lines4 = text.split(':', 1); var fromtime = dayjs().hour(lines4) var totime = dayjs().hour(lines9) const timedifference = (totime.diff(fromtime, "hours",)); expect(timedifference).to.equal(//value taken from another file, for example -2)
Advertisement
Answer
I notice you have the dayjs
library. To handle parsing strings with various, add the customParseFormat
plugin.
import dayjs from 'dayjs' import customParseFormat from 'dayjs/plugin/customParseFormat' dayjs.extend(customParseFormat)
Your dateString is passed into dayjs(dateString, format)
as 1st parameter and format as 2nd parameter.
The .hours()
method takes no parameters, it just extracts the hours portion.
I’m not sure exactly what format you have, so here’s a few examples.
I’ve lined up the dateString and format so you can see the pattern.
// "Standard" format const dateString1 = '2022-05-14 10:12:54' const date1 = dayjs(dateString1, 'YYYY-MM-DD HH:mm:ss') const hours1 = date1.hour() console.log(hours1) // logs 10 // Dates with time in "10:12:54 AM" format const dateString2 = '2022-05-14 10:12:54 AM' const date2 = dayjs(dateString2, 'YYYY-MM-DD HH:mm:ss A') const hours2 = date2.hour() console.log(hours2) // logs 10 const dateString3 = '2022-05-14 1:12:54 PM' const date3 = dayjs(dateString3, 'YYYY-MM-DD HH:mm:ss A') const hours3 = date3.hour() console.log(hours3) // logs 13 // Dates with time in "10AM" format const dateString4 = '2022-05-14 10AM' const date4 = dayjs(dateString4, 'YYYY-MM-DD HHA') const hours4 = date4.hour() console.log(hours4) // logs 10 const dateString5 = '2022-05-14 1PM' const date5 = dayjs(dateString5, 'YYYY-MM-DD HHA') const hours5 = date5.hour() console.log(hours5) // logs 13 // Time-only strings const timeString6 = '10:12:54 AM' const date6 = dayjs(timeString6, 'HH:mm:ss A') const hours6 = date6.hour() console.log(hours6) // logs 10 const timeString7 = '1:12:54 PM' const date7 = dayjs(timeString7, 'HH:mm:ss A') const hours7 = date7.hour() console.log(hours7) // logs 13
The full list of formats is here
I can’t work out the layout of fixture file from your code.
In general, don’t try to write and read intermediate values, it doesn’t help you.
If you can control the format of the fixture file, you should separate fromDate
and toDate
with a comma which can be used to split the line
For example, cypress/fixtures/dates.txt
2022-05-14 10:12:54 AM, 2022-05-14 1:12:54 PM ... etc
Test:
cy.readFile('cypress/fixtures/dates.txt').then((dates) => { const lines = dates.split('n') lines.forEach(line => { const fromDateString = line.split(',')[0].trim() const toDateString = line.split(',')[1].trim() const fromDate = dayjs(fromDateString, 'YYYY-MM-DD HH:mm:ss A') const toDate = dayjs(toDateString, 'YYYY-MM-DD HH:mm:ss A') const timeDifference = toDate.diff(fromDate, "hour") expect(timeDifference).to.equal(expectedDifference) }) })
Note: toDate.diff(fromDate, "hour")
will give a positive difference.
To compare with a negative difference, use the reverse
fromDate.diff(toDate, "hour")