How to set username variable with current time as part of the username

Hello! Iā€™m new to Postman. I just received a collection with variables., but I need better custom usernames for my tests.
Right now the Pre-request script looks like this:
pm.environment.set(ā€œrandomUserNameā€, ā€˜TestMUserā€™+currdate.valueOf())

but Iā€™m not happy with the usernames it makes, it doesnā€™t look like the current date at all, and overall too long, like this:
TestMUser1676290156475,
and Iā€™d like it to be like this:
TestMUser20230221999 or 21022023999

What can I do?
Thank you in advance and please excuse my poor English.

Hey there, welcome to the community! :wave:

You can use Dynamic Variables to do that: Dynamic Variables | Postman Level Up - YouTube

Quick tip for next time, try searching the forum first as these questions typically have already been answered :smiley:

1 Like

You can also use moment to format dates however you like.

Two examplesā€¦

const moment = require('moment');

let dateV1 = moment(new Date()).format("YYYYMMDD");
console.log("TestUser"+dateV1);

let dateV2 = moment(new Date()).format("YYYYMMDDssmmhh");
console.log("TestUser"+dateV2);

Version 2 includes the hours, minutes and seconds to ensure that its unique.

image

1 Like