timestampjqisodate

Using JQ to parse timestamp


I am having some difficulty finding my error with JQ parsing this timestamp.

"time": "2024-05-13 16:43:06.928708"

.time| strptime("%Y-%m-%d %H:%M:%S.%f")| strftime("%Y")

It clearly is not agreeing that I have this format, however this is exactly the format I'm using in Python.

here is the error message received. "2024-05-13 16:43:06.928708" does not match format "%Y-%m-%d %H:%M:%S.%f"

What gives, do I need some ticks somewhere?


Solution

  • While the Go implementation of jq actually can parse %f correctly:

    gojq '.time | strptime("%Y-%m-%d %H:%M:%S.%f") | strftime("%Y")'
    

    If all you need is %Y from a date string that is already formatted starting with %Y-, just extract its first four characters:

    jq '.time[:4]'
    

    Demo