Rafael Umbelino
Rafael Umbelino

Reputation: 25

Convert timestamp with month name to readable time stamp with jq

I have the following output when I query data through API to New Relic:

"timestamp": "1696358217197"

This is a timestamp prefixed with a month (This number is supposed to be October). I'm processing the information through JQ but I can't find a way to convert from this format to Year-Month-Day (ie: 2023-10-02).

Tried all the options I could find and I can't find neither a way to display the date correctly from New Relic API, nor to interpret it with JQ. I'm borderline using sed to replace the information since my script already accepts date as an argument.

Is there a better way?

Upvotes: 1

Views: 439

Answers (1)

pmf
pmf

Reputation: 36151

Given {"timestamp": "1696358217197"}, extract the string using .timestamp, convert to number using tonumber, divide by 1000 to convert milliseconds to seconds, and finally convert to your preferred date format using strftime (%F is equivalent to %Y-%m-%d):

jq -r '.timestamp | tonumber / 1000 | strftime("%F")'
2023-10-03

Demo

Upvotes: 2

Related Questions