Postman中使用格式化为YYYYMMDDHHmmssSSS的时间戳

在request中如何用到格式化为YYYYMMDDHHmmssSSS时间戳?

一、预处理,在Pre-requestScript写入以下脚本

//导入模块moment
var moment = require('moment');

//获取当前时间,格式化后赋值给变量entryTime
var entryTime = moment().format("YYYYMMDDHHmmssSSS");
//获取当前时间,格式化后赋值给变量exitTime
var exitTime = moment().format("YYYYMMDDHHmmssSSS");

//控制台打印变量
console.log(entryTime);
console.log(exitTime);

//将两变量值赋值给全局变量
pm.globals.set("entryTime", entryTime);
pm.globals.set("exitTime", exitTime);
  1. 首先引入moment库;
  2. 使用moment()方法获取当前时间,并格式化为YYYYMMDDHHmmssSSS的时间戳;
  3. 控制台打印语句是为了方便查看取值是否正确。如何调出Postman的控制台?View–>Show Postman Console
  4. 设置全局变量

二、在request body中使用全局变量

用两个大括号引用全局变量{{entryTime}}和{{exitTime}},执行后,控制台可以看到这两个变量的值。

 

 

【版权说明:仅允许非商业转载且请注明出处:Mac私塾 网址:http://macsishu.com】
  1. After checking out a few of the blog articles on your website, I really like your way of writing a blog. I added it to my bookmark webpage list and will be checking back in the near future. Take a look at my web site as well and tell me what you think. Dael Adolphe Cly

  2. Wow Dan and Lisa, I conjured you! Only an hour ago I was thinking of you guys and hoping you were weathering this storm and planning to rise up and head out into the wind and tell us all about your experiences. So happy to read your post just now! Sending good vibes for your upcoming ride! Aimil De Witt Leland

  3. The road seemed to stretch on endlessly before us, but Alexis assured me that our journey was near an end. We’d turn before long into thick woods and travel through narrow, winding roads until we reached her family’s cabin. I had no choice but to trust her as GPS had given out nearly 20 minutes ago.

    “There’s good wifi and okay reception at the cabin,” she’d told me, “but you can’t get there unless you know the way.”

    So here I was, driving alone in the middle of god knows where with a girl who was my student just a couple of weeks ago. Her and her four best friends had been together from first grade all the way through high school and now they had graduated with very different futures ahead of them. They had decided to kick off “the best summer ever” with a week long stay at Alexis’s family cabin. Alexis and I were heading up before everyone else, the four other girls and four guys.

发表评论

邮箱地址不会被公开。