I am writing cypress test case in which I have to upload a file. Once upload completes, the UI refreshes to show the upload time. I have to assert that the file upload time recorded is indeed correct. So I captured the date time displayed. It was Oct. 5, 2021, 3:29 a.m.. Also once upload completes I can get the current time in the test case with say new Date(). My doubt is how do I compare these two?
The date-time captured from the UI is a string, so you need to parse it to a date for comparison.
For example, using dayjs
Since the string is a slightly non-standard format, you can supply a format string for parsing.
The only thing that needs extra work is removing the dots in a.m.
const dateString = 'Oct. 5, 2021, 12:31 a.m.'
.replace('a.m.', 'am')
.replace('p.m.', 'pm')
const uploadDate = dayjs(dateString, 'MMM. DD, YYYY, HH:MM a') // parse
const now = dayjs()
expect(uploadDate.isSame(now, 'minute')).to.eq(true) // uploaded in last minute
// or
const diffMinutes = now.minute() - uploadDate.minute()
expect(diffMinutes).to.be.lte(1)