前言:postman脚本可使用js语法做一些前置处理或后置处理,这里的时间处理一般在Pre-request-Script中
//js获取当前时间戳的三种方法
var timestamp = Date.parse(new Date()); //1616658069000
var timestamp1 = (new Date().valueOf()); //1616658069310
var timestamp2 = new Date().getTime(); //1616658069310
var timestamp3 = Math.round(new Date().getTime()/1000); //1616662832
var timestamp0 =pm.variables.replaceIn("{{$timestamp}}") //最简单粗暴的方法,结果 1616666736
console.log(timestamp);
console.log(timestamp1);
console.log(timestamp2);
console.log(timestamp3);
console.log(timestamp0);
//js将时间戳转换为时间,也可以此基本上做一些加减,表示过去或未来时间
var t = new Date(1616661670810); //Thu Mar 25 2021 16:41:10 GMT+0800 (中国标准时间)
var t1 = t.getFullYear() + "-" + (t.getMonth()+1) + "-" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds(); //2021-3-25 16:41:10
console.log(t);
console.log("t",t1);
//格式化时间戳
var moment = require('moment');
var data = moment().format(" YYYY-MM-DD HH:mm:ss"); //2021-03-25 15:54:58
console.log(data);
//获取比当前时间多一天的时间戳
var moment = require('moment');
var day = moment().add(1,'days');
var stamp = Date.parse(day)
console.log(stamp)