Reputation: 456
We create a date format like that:releaseVersion=$(date +%y%m%d%H%M)
The result is 2105231450
How can I convert it back to iso format?
I'm trying date -d '2105031452' +'%y%m%d%H%M'
but getting date: invalid date ‘2105031452’
The final format should be 2021-05-23 14:50
How can I do that?
Upvotes: 0
Views: 1129
Reputation: 1321
A date is a string, possibly empty, containing many items separated by whitespace. The whitespace may be omitted when no ambiguity arises.
https://www.gnu.org/software/coreutils/manual/html_node/General-date-syntax.html#General-date-syntax
d0="2105031452"; date "+%Y-%m-%d %H:%M" -d "${d0:0:6} ${d0:7}"
Upvotes: 1
Reputation: 136
$ time=2105231450
$ date -d "${time:0:6} ${time:6:4}"
Sun 23 May 14:50:00 CEST 2021
$ date -d "${time:0:6} ${time:6:4}" +"%Y-%m-%d %H:%M"
2021-05-23 14:50
Upvotes: 1
Reputation: 84559
Since this is bash, another option is using the parameter expansion for substring indexes, e.g.
r=2105231450; isodate="20${r:0:2}-${r:2:2}-${r:4:2} ${r:6:2}:${r:8:2}:00"
Then:
$ date -d "$isodate"
Sun May 23 14:50:00 CDT 2021
Upvotes: 6
Reputation: 4455
On MacOS,
$ date -j -f %y%m%d%H%M 2105311209 +%Y-%m-%d\ %H:%M
2021-05-31 12:09
Note that -f is used to specify the input format.
Upvotes: 3
Reputation: 246847
Assuming the year occurs in this century, I'd use string manipulation:
$ echo '2105231450' | sed -E 's/(..)(..)(..)(..)(..)/20\1-\2-\3 \4:\5/'
2021-05-23 14:50
Upvotes: 4