I’m using an html template within the Tests tab to output my json data, however, some of the data is in timestamp form. Namely, the date_created, first_seen, & last_seen are all being returned in timestamp form. Is there a way to do the calculation to a more standard date/time format from within the var template ? I’ve tried several ways to convert the timestamp, but none work within the template variable or cause the call to fail. Is it even possible to do within that template? Or how would you do it outside the template and then pull it back into the html? Because of the each loop I’m just not sure how to do this.
The other thing to keep in mind is the number of number of results can vary, so it’s not a fixed number of results I can hard code.
I’m not sure you can call a function to change the date with your variable, as it will probably just save it as text as part of the overall string. (But I’ll have a go).
What is the current format of the date? And what does it need to look like?
You need to parse the response first to a JavaScript object which you can then update before using in the visualizer. The following will update the initial elements but not the ones in the “ns” array. You will probably need to extend the code to loop though the array and create a new array to then use in the visualizer. I can’t think of any other way to do this. This is more of a JavaScript problem than Postman. So if you know a JavaScript developer, I would lean on them
So it’s the same principle. The date is in seconds format, so needs to be converted first before using moment to format the date. (Or you can use the slice method if you only want the date).
Either by a function or the toISOstring method.
function toDateTime(secs) {
var t = new Date(Date.UTC(1970, 0, 1)); // Epoch
t.setUTCSeconds(secs);
return t;
}
let startDt = "1690675200"
var moment = require('moment');
// function method
var formattedDate = moment(toDateTime(startDt)).format("DD.MM.YYYY hh:mm"); // 30.07.2023 00:00
console.log(formattedDate);
// toISOstring method
let toISOstring = (new Date(startDt * 1000)).toISOString();
var formattedDate2 = moment(toISOstring).format("DD.MM.YYYY hh:mm"); // 30.07.2023 00:00
console.log(formattedDate2);
var moment = require('moment');
let startDt = (new Date(pm.response.json().tournament.blockHeader.startDt * 1000)).toISOString();
pm.response.json().tournament.blockHeader.startDt = moment(startDt).format("DD.MM.YYYY hh:mm");
pm.visualizer.set(template, {response: pm.response.json()});
You need to parse the response to a variable and manipulate that.
let response = pm.response.json();
Otherwise each time you call pm.response.json(), it will just get the original response. Which is why your date is still showing showing the original timestamp.
I don’t understand, where I need to use this parser
Now I use pm.visualizer.set(template, {response: pm.response.json()}) for vizualization
And I use response’s variable {{}} for all my parameters, not only for dates
Where do I need to use parser? Near pm.visualizer.set(template, {response: pm.response.json()}) ?
Or I can use it in html parts?
Sorry for stupid questions. I’m beginner in Postman
Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.