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

**URL:** <https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [March 31, 2023, 3:40am UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832 "2023-03-31T03:40:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [March 31, 2023, 3:40am UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832/1 "2023-03-31T03:40:39Z")

</div>

![微信图片_20230331113254](https://us1.discourse-cdn.com/getpostman/original/3X/d/f/df3d30eb460e59d5c86c597d999e9dae7495a355.png)

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!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 31, 2023, 10:14am UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832/2 "2023-03-31T10:14:44Z")

</div>

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](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries)

[Moment.js | Home (momentjs.com)](https://momentjs.com/)

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/5/2/52c3c7c536d56ac46749c926dafe356bac85b0aa.png)

---

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [April 2, 2023, 2:34pm UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832/3 "2023-04-02T14:34:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 3, 2023, 9:08am UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832/4 "2023-04-03T09:08:56Z")

</div>

I’m not really following you.

> [@lwj7](#):
>
> 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

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.

```auto
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.

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

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/7/b7e36da808fac1399b0f7142986e3dc0c10e8371.png)

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.

---

<div class="post-metadata">

**Author:** ![lwj7](https://avatars.discourse-cdn.com/v4/letter/l/54ee81/32.png) [@lwj7](https://community.postman.com/u/lwj7)\
**Post date:** [April 10, 2023, 12:56am UTC](https://community.postman.com/t/the-extracted-variable-is-year-month-day-hour-minute-and-second-how-to-convert-it-to-year-month-day/45832/5 "2023-04-10T00:56:22Z")

</div>

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