邮递员要求的前一天 [英] Previous day in Postman request
问题描述
邮递员的预请求脚本中包含以下代码-该代码可以显示当前日期。我宁愿要昨天的日期(current_timestamp-1天)。
I have below code in postman's pre-request script --which gives me current date. I rather want yesterday's date (current_timestamp - 1 day).
var current_timestamp = new Date();
postman.setEnvironmentVariable("current_timestamp", current_timestamp.toISOString());
我搜索了doc&净,但无法得到答案。有人可以帮我参考日期函数-获得我想要的结果。
I searched doc & net but could not get the answer. Can someone please help me with reference to date functions --to get my desired result.
谢谢
推荐答案
您可以使用Postman中的 momentjs 模块获取任何格式的日期
You can use the momentjs module in Postman to get a date in any format you need.
在 Pre-Request脚本
中,添加此内容即可获得所需的内容,而无需使用本机JS:
In the Pre-Request Script
, add this to get what you need without using native JS:
var moment = require('moment')
pm.environment.set("current_timestamp", moment().toISOString())
pm.environment.set("current_timestamp - 1 day", moment().subtract(1, 'day').toISOString())
此代码段将引入模块并在环境文件中设置所需的日期。
This snippet will bring in the module and set the dates you require in the environment file.
对于普通JavaScript中的非时刻
解决方案,只需快速返回24小时,您可以执行以下操作:
For a non moment
solution in plain JavaScript to just quickly go back 24hrs, you could do something like this:
var yesterday = (Date.now() - 86400000) // 24hrs in ms
pm.environment.set('yesterday', new Date(yesterday).toISOString())
这两种解决方案都可以为您带来相同的结果,但是我更喜欢使用moment,因为它是一个内置的模块,可以很好地处理日期和时间。
Both solutions would give you the same outcome but I prefer to use moment as it's a built-in module that handles dates and times very well.
这篇关于邮递员要求的前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!