4

The following command generates the date for the next day:

date -d "20150615 12:00 +1 day" +%Y%m%d
20150616

I would like to specify my own INPUT date format, such as:

2015_06_15

But the date command does not like this format and complains about invalid date:

date: invalid date '2015_06_15 12:00 +1 day'

Is it possible to use such a date format? And if so how could I do this.

Xofo
  • 1,256
  • 4
  • 18
  • 33
  • 2
    See: http://stackoverflow.com/questions/20691852/linux-bash-parse-date-in-custom-format - the date command does not have an input format specifier, you would have to convert the input into something date understands by default. – Matthew Jun 16 '15 at 18:30
  • 3
    GNU `date` does not allow you to specify an input format; the full documentation for what *is* accepted can be found [here](https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html#Date-input-formats). – chepner Jun 16 '15 at 19:10
  • Thank you. I wonder if a proposal can be floated to add such a feature to GNU date ... – Xofo Jun 16 '15 at 20:30

1 Answers1

6

A workaround:

x="2015_06_15"
date -d "${x//_/} 12:00 +1 day" +%Y%m%d

Output:

20150616
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • For others wondering like myself what x//_/ means this is called expansion found in zsh manual 14.3 Parameter Expansion: ex.1${name/pattern/repl} ex.2 ${name//pattern/repl} - replace all occurances of pattern "_" with "" so it truncates the input date to 20150615 which is a readable format. ex.3${name:/pattern/repl} – nwood21 Mar 16 '21 at 01:58