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.
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();
dd = '0'+dd
dd_end = '0'+ dd_end
mm = '0'+mm
hh = '0'+hh
ss = '0'+ss
today = yyyy +'-'+ mm +'-'+ dd + 'T' + hh + ':' + ss + 'Z';
enddate = yyyy +'-'+ mm +'-'+ dd_end + 'T' + hh + ':' + ss + 'Z';