Hey Guys,
I need a pm.test to calculate the current date/time and makes sure it doesn’t go more 1 day
const jsonData = pm.response.json ();
const date1 = jsonData.test.validateDate (validateDate is a field in my response)
const date2 = not to go beyond 24 hours
Pm test
pm.expect(Date.parse(date1)).to.be.above(Date.parse(date2));
Format date/time e.g 2023-02-10T17:40:51.816
I would recommend using the Moment library which is available in the Postman Sandbox.
Try the following.
const moment = require('moment');
let now = moment(new Date()); //todays date and time
// console.log(now);
let responseDate = "2023-02-10T15:40:51.816"
// console.log(responseDate);
let duration = moment.duration(now.diff(responseDate)).asHours();
// console.log(duration);
pm.test("Response date to be less than 24 hours", function () {
pm.expect(duration).to.be.lessThan(24);
});
You might want to test around the boundaries to ensure its doing what you want.
You also might want to consider the following test as this failed originally as I’m an hour behind you.
pm.test("Response date to be less than 24 hours", function () {
pm.expect(duration).to.be.above(0).and.lessThan(24);
});