The extracted variable is year, month, day, hour, minute and second. How to convert it to year, month, day

As shown in the figure, the value of the productionDate field has been successfully extracted. Because the stored values are years, months, days, hours, minutes, and seconds, the extracted values are also years, days, hours, minutes, and seconds. How do I convert extracted values to years, months, and days? Or when extracting variables, how to intercept only the year, month, and day, but not the hour, minute, and second, thank you!

The tests tab is basically JavaScript code, so when faced with these types of queries, its a case of Googling how it would be done in JavaScript.

Postman supports a number of JavaScript libraries within its sandbox, and this includes moment, which is useful for working with dates.

Postman JavaScript reference | Using External Libraries

Moment.js | Home (momentjs.com)

let productionDate = "2023-02-16 00:00:00"
console.log(productionDate);

var moment = require('moment');
var newDate = moment(productionDate).format("YYYYMMDD");
console.log(newDate);

image

Sorry, that still doesn’t solve the problem
Because in your reply, let productionDate = “2023-02-16 00:00:00”, but the productionDate returned by each interface does not have to be 2023-02-16 00:00:00.
I need to access the interface first, get the corresponding return data, extract the productionDate variable from the interface, and convert it to year/month/day
What I’m doing now is I’m accessing the interface and extracting the productionDate value setting variable from the response
var Jsondata = JSON.parse(responseBody);
pm.globals.set (productionDate, Jsondata.data.productionDate);
Now, the value of the extracted variable is 2023-02-16 00:00:00 and cannot be converted to 2023-02-16 as you suggested

I’m not really following you.

The extracted variable from your response is the same format as I’ve defined in my code block. I don’t have access to your response, so I need to declare a variable in the same format to show the working example. You would replace this with your code retrieving the actual date from the response.

If you want the hyphens in the date, then its a minor change to produce this. However, using moment, you are in full control of what format you want the date to look like.

let productionDate = "2023-02-16 00:00:00"
console.log(productionDate);

var moment = require('moment');
var newDate = moment(productionDate).format("YYYY-MM-DD");
console.log(newDate);

If you really want to, you can break it down into individual components.

let day = moment(productionDate).format("DD");
let month = moment(productionDate).format("MM");
let year = moment(productionDate).format("YYYY");
console.log(`${year}-${month}-${day}`);

image

Moment is pretty clever at working out the initial date format, so it shouldn’t matter too much if the initial date is in a slightly different format.

According to your method, the problem has been solved, thank you