首页 > 解决方案 > Postman - Need help to store a variable from the date value(YYYY-MM-DD) in response by changing the date format to MMM-DD-YYYY

问题描述

I am getting my output in (YYYY-MM-DD). i want to save to an environment variable by changing the date format to MMM-DD-YYYY (Feb-26-2020)

Sample response:

{   
     "pickupDate": "2020-02-26",
     "cutOffTime": "20:00:00",
     "accessTime": {
         "hours": 1,
         "minutes": 0
     },
} 

Using below just to save the date as it is. But help me to change and save it.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("day1", jsonData.output.options[1].pickupDate);

标签: postmandate-format

解决方案


You should be able to do that in the Tests tab of the Postman app using moment:

const moment = require('moment')

let jsonData = pm.response.json();
pm.environment.set("day1", moment(jsonData.output.options[1].pickupDate).format('MMM-DD-YYYY'));

You would need to bring in the module first and then use .format() to change the date to what you want. I've also used the newer Postman syntax as you were still using the older syntax.


推荐阅读