If you are a developer or technologist like me, chances are you are very familiar with Postman for working with RESTful APIs. Depending on what types of APIs or services you work with, there are many use cases or instances when a date / time will be required in a given request. Lucky for us, the folks at Postman have made pre-request scripts available which cna be utilized in a number of different ways to dynamically set variables before a given request is executed.

In this post, we are going to look at how you can dynamically set an ISO formatted date time to the current time without having to manually set the value.

[cc lang=”javascript”]

var today = new Date();
var dd = today.getDate();
var dd_end = today.getDate()+2; //two days ahead for end date
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var hh = today.getHours();
var ss = today.getSeconds();
if(dd<10) {
dd = ‘0’+dd
if(dd_end<10) {
dd_end = ‘0’+ dd_end
if(mm<10) {
mm = ‘0’+mm
if(hh<10) {
hh = ‘0’+hh
if(ss<10) {
ss = ‘0’+ss
today = yyyy +’-‘+ mm +’-‘+ dd + ‘T’ + hh + ‘:’ + ss + ‘Z’;
postman.setEnvironmentVariable(“date”, today);
enddate = yyyy +’-‘+ mm +’-‘+ dd_end + ‘T’ + hh + ‘:’ + ss + ‘Z’;
postman.setEnvironmentVariable(“enddate”, enddate);


Recommended Posts

No comment yet, add your voice below!

Add a Comment

Your email address will not be published. Required fields are marked *