How can I covert this date format in bash
Sat Jul 17 19:07:29 2021 +0200
To
YYYY/MM/DD hh:mm:ss
2021/07/17 19:07:29
If you are using a Unix program, you can often convince it to generate the output in the expected format instead of later trying to process it.
For example, you can do this with git log. Here's what it looks like on a repo:
$ git log --format=%ad --date='format:%Y/%m/%d %H:%M:%S' | head
2021/07/05 22:45:03
2021/07/05 22:20:49
2021/07/05 18:08:33
2021/06/09 11:18:40
2021/05/25 11:48:31
2021/05/21 18:20:40
2021/05/20 17:05:17
2021/05/20 16:53:24
2021/05/20 16:29:13
2021/05/20 16:18:24
This relies on telling git log to format entries to only show the date using --format=%ad and then specifying the format of the date you want using --date='format:%Y/%m/%d %H:%M:%S'. See man git log for more details and man date for the format spacifiers you can use with --date=format:.
Sat Jul 17 19:07:29 2021 +0200as the input string? Or are you getting it by runningdateyourself? In other words, are you more interested in converting one string to another, or are you interested in generating the correct string in the first place?date -d 'Sat Jul 17 19:07:29+0200 2021' '+%Y/%m/%d %T', (or-Isinstead of'+%..'for iso format). seedate --helpfor details orman date