momentjs

https://momentjs.com/


Install

npm install moment --save

Use in Project

import moment from 'moment'

CurrentTime

var dateTime = moment()
var dateString = dateTime.format('YYYYMMDDHHmm') // minute

Convert Server UTC time to Local time

In date storage, server time is UTC.

When to display on pages, we need to convert server UTC time to local time.

var local = moment(res.date).utcOffset(8).format('YYYY-MM-DD HH:mm:ss')

When time is Not Accurate

If you want assert a range when time is not accurate.

const start = '2020/12/21'
const end = '2021/03/21'
const format = 'YYYY/MM/DD'
const num = moment(start, format).diff(moment(end, format), 'days')
console.log(num) // 90

In Cypress, I often write like this.

cy.get(selector).then(($el) => {
var time = $el.text()
var endTime = '2022-01-03 15:02:53'
expect(moment(time).diff(moment(endTime))).to.gte(0) // after
expect(moment(time).diff(moment(endTime))).to.lte(3000) // 3000ms
})