2

I need to convert seconds to date:

My input documents are:

{"_id":"ae53d3ec-8fc3-44fc-a7eb-f2f32da4eaae","birthDate":{"value":{"$date":{"$numberLong":"-469929600000"}}}}
{"_id":"ef92c3e4-d5d7-4b81-8a0b-1ab1eac10331","birthDate":{"value":{"$date":{"$numberLong":"-854755200000"}}}}

I need to get:

{"_id":"ae53d3ec-8fc3-44fc-a7eb-f2f32da4eaae","birthDate":"1955-02-10"}
{"_id":"ef92c3e4-d5d7-4b81-8a0b-1ab1eac10331","birthDate":"1942-12-01"}

Any ideas?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • Does this answer your question? [jq dates and unix timestamps](https://stackoverflow.com/questions/36853202/jq-dates-and-unix-timestamps) – Barbaros Özhan Feb 08 '22 at 20:25

2 Answers2

2

Grab the source value, chop off the last three digits to turn the number into seconds and call todate to convert it into a date string (GMT).

jq -c '.birthDate |= (.value."$date"."$numberLong"[:-3] | tonumber | todate[:10])'
{"_id":"ae53d3ec-8fc3-44fc-a7eb-f2f32da4eaae","birthDate":"1955-02-10"}
{"_id":"ef92c3e4-d5d7-4b81-8a0b-1ab1eac10331","birthDate":"1942-12-01"}

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31
1
.birthDate |= (
   .value."$date"."$numberLong" |
   tonumber |
   . / 1000 |
   strftime("%F")
)

With -c, this produces the desired output exactly.

{"_id":"ae53d3ec-8fc3-44fc-a7eb-f2f32da4eaae","birthDate":"1955-02-10"}
{"_id":"ef92c3e4-d5d7-4b81-8a0b-1ab1eac10331","birthDate":"1942-12-01"}

Demo on jqplay

ikegami
  • 367,544
  • 15
  • 269
  • 518