Local date time during Monitoring

I am trying to get the local date\time when running a collection via monitroing. I need to compare a date I get from my API response and see if it is the same as the current date. I’ve tried using various moment.js formats, as well as plain JavaScript date but get different results when running locally and running via my monitor.

For example, if I run the following code on 1\14\2021 @3:04 PM EST locally:

const date = moment.utc().format('YYYY-MM-DD HH:mm:ss');
console.log(date); 
const stillUtc = moment.utc(date).toDate(); 
console.log (stillUtc);
const local = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');
console.log(local); 

const jsLocal = new Date().toLocaleString()
console.log(jsLocal);

I get the following output:

2021-01-14 20:04:04
Thu Jan 14 2021 15:04:04 GMT-0500 (Eastern Standard Time) 
2021-01-14 15:04:04
1/14/2021, 3:04:04 PM

But if I run the same code via my monitor, I will get the following output:

2021-01-14 20:05:16
2021-01-14T20:05:16.000Z
2021-01-14 20:05:16
1/14/2021, 8:05:16 PM

Note each date is returned in UTC time when running via the monitor.

The problem occurs because I run my monitor at 10 PM EST nightly. When running at 10 PM EST on 1/13/2021, I will get the following returned:

2021-01-14T03:07:22.567Z
2021-01-14 03:07:22
2021-01-14T03:07:22.000Z
2021-01-14 03:07:22

If I wanted to test that the date is 1/13/2021 it will fail.

Does anyone have any workaround or another way that I could test for today’s date, keeping in mind I run my monitor at 10 PM EST nightly.

Thanks in advance.

Hey @jdeberadinis! Welcome to the community :wave:

I understand you would like to validate on the timestamp when the monitor is being executed, is that correct?

I think something like a code below might work for your purpose.

const convertTimeZone = (timeZone) => new Date().toLocaleString('en-US', {timeZone})

const now = convertTimeZone('America/Los_Angeles')

pm.test('Check the current time', () => {
    pm.expect(now).to.include('1/14/2021')
})

Hope this helps! Please feel free to reach out if you have further questions :slightly_smiling_face:

Hi @taehoshino ! Thanks so much for the response! This did solve my issue with getting the correct timestamp when running during monitoring. I am hoping it will resolve the date check I am trying to do. The test will run tonight, and I should be able to tell if my overall issue is resolved. If not, I may reach back out for some more help. Thaks!

@jdeberadinis Awesome! Thanks for letting me know! :smile: