Parthiban
Parthiban

Reputation: 111

How to get current year in the Postman script

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

Answers (4)

Gayatri Purohit
Gayatri Purohit

Reputation: 1

var currentYear=new Date().getFullYear();
postman.setEnvironmentVariable("currentYear" ,  currentYear);

This worked for me.

Upvotes: 0

Parthiban
Parthiban

Reputation: 111

Another way to get current year :

new Date().getFullYear();

Upvotes: 0

Dev
Dev

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());

enter image description here

Upvotes: 0

Arun Prasat
Arun Prasat

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

Related Questions