How to convert an epoch time in millisecond into an ISO like string according to local time?
Solution 1:
If your system/environment implements the placeholder %N
for nanoseconds, you could use %3N
to get the padded milliseconds:
TZ=America/New_York jq -r '
def mydate:
./1000 | strflocaltime("%Y-%m-%d %H:%M:%S.%3N");
mydate
' <<< 1642812926681
2022-01-21 19:55:26.681
No Demo (jqplay.org does not support the %N
placeholder)
If it doesn't, you could still do some tweaks to shorten it:
- omit calculating the
floor
for the whole seconds asstrflocaltime
will take care of it - replace
%Y-%m-%d %H:%M:%S
with%F %T
- use string interpolation
"\(…)"
for easier string conversion
TZ=America/New_York jq -r '
def mydate:
(./1000 | strflocaltime("%F %T.")) + "00\(.%1000)"[-3:];
mydate
' <<< 1642812926681
2022-01-21 19:55:26.681
Demo