How best to extract a date/time from a JSON response to use as a date/time object

My question:
I receive a JSON response similar to:

{
   ...
    "startTime": "Wed, 19 Apr 2023 09:48:40 GMT",
    "expirationTime": "Tue, 30 May 2024 17:19:31 GMT",
   ...
}

I want to do some time manipulation on these dates, specifically on the expiration date and subtract now() to determine the time remaining. I can extract the data from the JSON response via a Test, using:

const responseJson = pm.response.json();
const expirationTime = responseJson.expirationTime;

However, I have no idea how to change that from a human-readable string to a date/time object.

I’m not sure if there is a Postman function to convert that string directly to data/time, or if I need to parse in some other way. I couldn’t seem to find much in the docs.

What do you mean by date\time object?

Your response looks like a pretty standard JavaScript date\time.

Postman uses JavaScript under the hood and also has access to the moment library which will allow you to format dates however you like.

It also has functions to do the subtractions for you.

Moment.js | Home (momentjs.com)

TIP: If you are only interested in the dates, use moment to format it as YYYYMMDD and parse it to an integer as this then becomes a number comparison at this point.

Example…

var moment = require('moment');
var today = parseInt(moment(new Date()).format("YYYYMMDD"));

Thanks, @michaelderekjones. I am not great with Javascript either :expressionless:

By “object”, I meant data type that I could manipulate easily. Currently, expirationTime is a string, so getting into a date/time data type or converting it into something that could be manipulated was what I was after.

I tried:

const responseJson = pm.response.json();
const expirationTime = responseJson.expirationTime;

var moment = require('moment');
var expired = parseInt(moment(expirationTime));

console.log(expired);

Although this results in:

NaN

Urgh, idiot! Trying to parse the returned moment sting into an Integer is not going to work :face_exhaling: . So, this looks fine:

const responseJson = pm.response.json();
const expirationTime = responseJson.expirationTime;

var moment = require('moment');
var expired = moment(expirationTime);

console.log(expired);

The moment example I provided was quick and dirty to return todays date.

I used the format option within moment to return just the date elements in YYYYMMDD format.

This will return a string but as it will contain numbers, you can parse it using parseInt.

If you don’t format the date, then it will include the date, time and the timezone which includes letters, so you can’t parse it directly to a number.

As far as I can tell, you need two variables.

One for today, and one for your expirationDate, they both need to be in the same format.

Once they are in the same format, you can do a comparison, count the days, etc.

If they are both in YYYYDDMM format, it is just a number comparison to work out the difference in days.

You don’t have to convert it to YYYYDDMM. Moment also has calculations that you might want to consider.

Thanks again, @michaelderekjones - I think I now have something nearly workable. This might be a bit ugly/cumbersome:

const responseJson = pm.response.json();
const expirationTime = responseJson.expirationTime;
// const timeFormat = "ddd, DD MMM YYYY hh:mm:ss" 

var moment = require('moment');
var expiration = moment(expirationTime);
var today = moment();

console.log(expiration);
console.log(today);

diffSeconds = expiration.diff(today, 'seconds');
diffMomentMS = moment.utc(moment.duration(diffSeconds, "seconds").asMilliseconds());

console.log("Time left: " + diffMomentMS.format("DD") + " Days, " + diffMomentMS.format("hh") + " Hours, " + diffMomentMS.format("mm") + " Minutes, " + diffMomentMS.format("ss") + " Seconds." );

Output:

2023-08-12T19:36:55.000Z
2023-07-03T18:46:15.890Z
Time left: 10 Days, 12 Hours, 50 Minutes, 39 Seconds.

Unfortunately, the days left should be 39, so I’m not quite there

In fact that calculation is garbage :face_exhaling:

Right, slightly different tack using modulo maths:

const responseJson = pm.response.json();
const expirationTime = responseJson.expirationTime;
// const timeFormat = "ddd, DD MMM YYYY hh:mm:ss" 

var moment = require('moment');
var expiration = moment(expirationTime);
var today = moment();

console.log("Expiration date (Epoch time in milliseconds): " + expiration);
console.log("Todays date (Epoch time in milliseconds): " + today);

var diffSeconds = expiration.diff(today, 'seconds');

var numDays = Math.floor(diffSeconds / (24 * 3600));
var numHours = Math.floor((diffSeconds % (24 * 3600)) / 3600);
var numMinutes = Math.floor((diffSeconds % (3600) / 60));
var numSeconds = Math.floor(diffSeconds % 60 );

console.log("Time left: " + numDays + " Days, " + numHours + " Hours, " + numMinutes + " Minutes, " + numSeconds + " Seconds." );

Output:

Expiration date (Epoch time in milliseconds): 1691869015000
Todays date (Epoch time in milliseconds): 1688413437410
Time left: 39 Days, 23 Hours, 52 Minutes, 57 Seconds.

const responseJson = pm.response.json();
const expirationTime = new Date (responseJson.expirationTime;).getTime();
This will return the this time in Milliseconds (int).

1 Like