Reputation: 111
I have a requirement to get current year in the Postman pre-req script.
I'm planning to get the current date, then convert the date to string and apply sub-string to get year value
I would like to know, is this the right way of doing it, or is there any pre-defined function available to do it?
Upvotes: 1
Views: 2880
Reputation: 1
var currentYear=new Date().getFullYear();
postman.setEnvironmentVariable("currentYear" , currentYear);
This worked for me.
Upvotes: 0
Reputation: 2813
To get year -
var moment = require('moment');
console.log("timestamp", moment().format("YYYY"));
or even without using moment library -
console.log(new Date());
Upvotes: 0
Reputation: 360
I guess there is no pre-defined function to get year alone may be you can try like below,
const moment = require('moment');
pm.globals.set("timestamp", moment().format("MM/DD/YYYY"));
Then reference {{timestamp}} where ever you need it. check the link for more details
Upvotes: 2