jq dates and unix timestamps jq dates and unix timestamps json json

jq dates and unix timestamps


Sure! Your provided input is not valid JSON, but I'm going to assume the trailing commas on those objects are removed and the objects are wrapped in an array, which would be the root object of the JSON document.

First, we can transform the millisecond-precision UNIX dates into second-precision, which is what jq's date functions expect, and then convert that to the human-readable dates you expect:

.[].published_at |= (. / 1000 | strftime("%Y-%m-%d"))

Then, we select only those elements whose dates match:

map(select(.published_at == $date))

Lastly, we put it all together, taking the $date variable from the command-line:

jq --arg date "2016-04-25" '.[].published_at |= (. / 1000 | strftime("%Y-%m-%d")) | map(select(.published_at == $date))' stuff.json


jq 1.5 has standard time-and-date functions such as strftime, as documented in the online manual. However support for TZ is extremely limited and/or unreliable, as illustrated here:

$ echo $TZ$ jq -n '123 | strftime("%B %d %Y %I:%M%p %Z")'"January 01 1970 12:02AM EST"TZ='Asia/Kolkata' jq -n '123 | strftime("%B %d %Y %I:%M%p %Z")'"January 01 1970 12:02AM IST"

strflocaltime

If your jq has strflocaltime:

TZ=Asia/Kolkata jq -n '123|strflocaltime("%Y-%m-%dT%H:%M:%S %Z")'"1970-01-01T05:32:03 IST"