首页 > 解决方案 > 如何在 Postman 中获取不同时区的当地时间?

问题描述

我想获取罗马时区的当地时间。在这里postman_sandbox_api_reference中没有找到关于如何在邮递员文档中使用时刻沙箱内置库的任何详细信息

到目前为止我尝试过的

var moment = require('moment');
console.log(moment().tz(environment.TimeZone).format());

它抛出的错误 - TypeError | moment(...).tz is not a function

又一次尝试——

var moment = require('moment-timezone');
console.log(moment().tz(environment.TimeZone).format());

它抛出的错误 -Error | Cannot find module 'moment-timezone'

我哪里错了?谁能指出我正确的方向。

谢谢

标签: postmanmoment-timezonepostman-testcase

解决方案


Postman 只有moment内置库,没有moment-timezone.

如果您正在做的事情不是moment文档的一部分,那么它将无法正常工作。

https://momentjs.com/docs/

作为获取数据的一种解决方法,您可以使用简单的 3rd 方 API。

向此端点发出请求将为您提供一些您可以使用的时区数据。

http://worldtimeapi.org/api/timezone/Europe/Rome

这可以添加到pm.sendRequest()pre-request script以获取您需要的数据并在另一个请求中使用它。

pm.sendRequest("http://worldtimeapi.org/api/timezone/Europe/Rome", function (err, res) {
    pm.globals.set("localTimeRome", res.json().datetime);
});

推荐阅读