Accessing Current Time & Different Timezone

1a. Accessing Current Time in Cypress:

We have to install the npm moment dependencies in order to access the Cypress.moment syntax.

Reference: https://momentjs.com/

Pre-requirements: 1. Install:

$ npm install moment --save-dev

Save in /cypress/support/index.js:

hello.sh
 Cypress.moment.locale('en');

Examples: Let's get the Current time using Cypress.moment()

Get Current Time in AM/PM

const nowTime = Cypress.moment().format('LTS')
cy.log('Current Timezone', nowTime)

Response: 1:47:33 PM Get Current Time in 24hrs

const now24Time = Cypress.moment().format('H:m:s')
cy.log('Current Timezone in 24hrs', now24Time)

Response: 13:47:33

Get Current time in 24hours format & Adding 10 minutes

const addTenMintues24hrs = Cypress.moment().add(10, 'minutes').format('H:m:s')

cy.log('Current Timezone in 24hrs format & add 10 Mints', addTenMintues24hrs)

Get Current time in am/pm & Adding 10 minutes

const addTen = Cypress.moment().add(10, 'minutes').calendar()
cy.log('Current Timezone in AM/PM & add 10 Mints', addTen)

Get the Current date & Add 10 days

const addTenDays = Cypress.moment().add(10, 'days').calendar()
cy.log('Current Timezone in AM/PM & add 10 Days', addTenDays)

1b) Accessing Time Zones in Cypress: Additional to the above dependencies, We have to install the npm moment-timezone dependencies in order to work with moment.tz() Pre-requirements: 1. Install:

npm install moment-timezone -- save

Save in /cypress/support/index.js:

Cypress.moment = require('moment-timezone');

Examples: Let's get time from the specified Timezone using Cypress.moment() Reference: https://momentjs.com/timezone/ Get CEST Timezone in Am/PM Here we are referring to CEST (Europe/Stockholm) Timezone

const CESTtimezone = Cypress.moment().tz('Europe/Stockholm').format('LTS')
cy.log('CEST Timezone in Am/PM', CESTtimezone)

Response: 6:47:33 AM Get CEST Timezone in 24hrs

const CESTtimezone = Cypress.moment().tz('Europe/Stockholm').format('LTS')
cy.log('CEST Timezone in Am/PM', CESTtimezone)

Response:06:47:33 Get time in 24 hours format & Adding 10 minutes

const addTenMinutes24hrsCEST = Cypress.moment().tz('Europe/Stockholm')
                                      .add(10, 'minutes')
                                      .format('HH:mm:ss')
cy.log('CEST Timezone in 24hrs & add 10mints', addTenMinutes24hrsCEST)

Response:06:57:33 TimeZone Parse and display dates: Reference: https://momentjs.com/timezone/ Time display Formats: Reference: https://momentjs.com/docs/#/displaying/

Last updated

Was this helpful?