0

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?

Parthiban
  • 43
  • 1
  • 11

4 Answers4

2

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

Arun Prasat
  • 340
  • 1
  • 9
0

To get year -

var moment = require('moment');
console.log("timestamp", moment().format("YYYY"));

or even without using moment library -

console.log(new Date());

enter image description here

Dev
  • 2,739
  • 2
  • 21
  • 34
0

Another way to get current year :

new Date().getFullYear();
Parthiban
  • 43
  • 1
  • 11
0
var currentYear=new Date().getFullYear();
postman.setEnvironmentVariable("currentYear" ,  currentYear);

This worked for me.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83