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

JSONPath Plus查询结果格式化咨询:日期格式转换

Formatting JSONPath Plus Date Results to DD/MM/YYYY

Absolutely! JSONPath Plus itself doesn’t include built-in date formatting features, but it’s straightforward to post-process the returned date components into your desired DD/MM/YYYY format using code (since JSONPath Plus is most commonly used in JavaScript environments). Here’s how to do it:

Step 1: Extract Date Components with JSONPath

First, use your existing query to pull the day, year, and month values. Depending on your JSON structure, the query $..CONTRACT_AWARD_DATE.* will return these components as an array (the order depends on how the keys are defined in your JSON). For example, if your data looks like this:

{
  "awards": {
    "CONTRACT_AWARD_DATE": {
      "day": "08",
      "year": "2011",
      "month": "05"
    }
  }
}

Your JSONPath query will return ["08", "2011", "05"].

Step 2: Rearrange and Format the Components

Once you have the array of date parts, rearrange them to match your desired DD/MM/YYYY structure and concatenate with slashes. If your parts are strings (already padded with leading zeros), you can do this directly:

// Assume jsonPath is the JSONPath Plus function
const dateParts = jsonPath(yourData, "$..CONTRACT_AWARD_DATE.*");
const formattedDate = `${dateParts[0]}/${dateParts[2]}/${dateParts[1]}`;
console.log(formattedDate); // Output: "08/05/2011"

Handling Numeric Values (No Leading Zeros)

If the query returns numeric values (e.g., [8, 2011, 5] instead of strings), add a helper function to pad single-digit days/months with leading zeros:

// Helper to ensure two-digit values
const padZero = num => num.toString().padStart(2, '0');

const dateParts = jsonPath(yourData, "$..CONTRACT_AWARD_DATE.*");
const formattedDate = `${padZero(dateParts[0])}/${padZero(dateParts[2])}/${dateParts[1]}`;
console.log(formattedDate); // Output: "08/05/2011"

Note on Component Order

Double-check the order of elements in the dateParts array! If your JSON structure stores the date components in a different order (e.g., month first), adjust the array indices accordingly to match your desired format.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:35:41