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

如何用jq将日期解析为时间戳?附现有脚本与输出示例

Convert ISO 8601 Date to Timestamp in jq for Bittrex API Output

Problem

I'm using this script to fetch tick data from Bittrex API and format the output:

curl -s -S 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NBT&tickInterval=thirtyMin&_=1521347400000' | jq -r '.result|.[] |[.T,.O,.H,.L,.C,.V,.BV] | @tsv | tostring | gsub("\\t";" ,") | "(\(.))"'

Current output includes ISO 8601 dates like this:

(2018-03-17T18:30:00,0.00012575,0.00012643,0.00012563,0.00012643,383839.45768188,48.465051)
(2018-03-17T19:00:00,0.00012643,0.00012726,0.00012642,0.00012722,207757.18765437,26.30099514)

I need to convert the date field (.T) into a Unix timestamp instead.

Solution

You can use jq's strptime() and mktime() functions to parse the ISO date string and convert it to a timestamp. Here's the adjusted script:

curl -s -S 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NBT&tickInterval=thirtyMin&_=1521347400000' | jq -r '.result|.[] | [(.T | strptime("%Y-%m-%dT%H:%M:%S") | mktime), .O, .H, .L, .C, .V, .BV] | @tsv | gsub("\\t";" ,") | "(\(.))"'

Key Changes Explained

  • strptime("%Y-%m-%dT%H:%M:%S"): Parses the ISO 8601 date string into an array of time components (year, month, day, hour, minute, second, timezone offset) based on the specified format.
  • mktime: Converts the time component array into a Unix timestamp (number of seconds since 1970-01-01 UTC). If you need a millisecond timestamp, just multiply the result by 1000: mktime * 1000.

Example Output

With the modified script, your output will look like this (using second-level timestamps):

(1521306600,0.00012575,0.00012643,0.00012563,0.00012643,383839.45768188,48.465051)
(1521308400,0.00012643,0.00012726,0.00012642,0.00012722,207757.18765437,26.30099514)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:33:53