Thursday, April 14, 2022

[SOLVED] Linux/Debian equivalent of macOS's date -jf, set source date format?

Issue

I have a date in the format of 201908270700 but need to change it to yyyy/mm/dd hh:mm, when googling I find lots of solutions to do this with the macOS command line, but not many for linux, at least not a simple solution.

On macOS you can do date -jf "%Y%m%d%H%M" "201908270700" "+%Y/%m/%d %H:%M" and it'll return 2019/08/27 07:00, but looking at the GNU date man page, there is no equivalent option on Linux, or is there?


Solution

With the date in a variable, it's easy to format into a form the GNU coreutils date(1) understands:

$ dt=201908270700
$ date -d"${dt:0:8} ${dt:8:4}" "+%Y/%m/%d %H:%M"
2019/08/27 07:00

This splits the date and time up into separate elements, which can then be parsed according to the rules for pure numbers in the coreutils documentation.



Answered By - Shawn
Answer Checked By - Clifford M. (WPSolving Volunteer)