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:
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");

let dateV2 = moment(new Date()).format("YYYYMMDDssmmhh");

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


1 Like