Postman get date using js

When using postman for interface automation testing, a query interface needs to use the date parameter to request;

Assuming that the current date is 2018-05-07 10:30:20, the date to be passed is:

startTime:2018-05-01 00:00:00;

endTime:2018-05-07 23:59:59;

function beginTime(){

  //Get the current date
  var date = new Date();

  //Get 6 days before the current date;
  date.setDate(date.getDate()-6);
  var Y = date.getFullYear()+'';
  var M = (date.getMonth()+1 < 10 ? '0 '+(date.getMonth()+1):date.getMonth()+1+'');//Get the current month, the month in js starts from 0, so it needs to be +1;
  var D = (date.getDate( ) < 10 ? '0'+date.getDate():date.getDate()+'');
  return Y+'-'+M+'-'+D+' '+'00:00:00';
}
pm. environment.set("beginTime",beginTime());

function endTime(){
    var date = new Date();
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1+'');
    var D = (date.getDate() < 10 ? '0'+date.getDate():date.getDate()+'');
    return Y+'-'+M+'-'+D+' '+'23:59:59';
}
pm.environment.set("endTime",endTime());

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560454&siteId=291194637