You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSON对象中设置sysdate变量并通过pre-req.脚本自动赋值?

Postman前置脚本动态设置请求体日期字段方案

操作步骤

  • 第一步:修改请求Body的硬编码值为变量占位符
    将你原来的请求体替换为如下内容,用{{currentSigningDate}}作为待填充的日期变量:
{
  "targetSigningDate": "{{currentSigningDate}}"
}
  • 第二步:添加Pre-request Script脚本
    打开当前请求的「Pre-request Script」标签,粘贴以下代码即可实现每次请求自动取当前系统时间赋值:
// 生成和原格式完全匹配的UTC标准ISO 8601时间,带毫秒后缀
const currentSysdate = new Date().toISOString();
// 将生成的时间存入临时变量,自动替换请求体中的占位符
pm.variables.set("currentSigningDate", currentSysdate);

可选扩展(非UTC时区需求)

如果需要使用指定时区的当前时间而非UTC时间,可将上述脚本替换为对应时区的生成逻辑,以下是东8区(北京时间)的示例:

function getLocalISO8Time() {
  const now = new Date();
  // 东8区偏移量为8小时,换算为毫秒计算
  const localTime = new Date(now.getTime() + 8 * 3600 * 1000);
  return localTime.toISOString().replace('Z', '+08:00');
  // 如果仍然需要末尾带Z的格式,可改为下面的写法
  // return new Date(now.getTime() - now.getTimezoneOffset() * 60000 + 8 * 3600 * 1000).toISOString()
}
pm.variables.set("currentSigningDate", getLocalISO8Time());

内容的提问来源于stack exchange,提问作者Lexamenrf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 22:57:03