bluecricket
bluecricket

Reputation: 2042

Can't increment date with ddmmyyyy format in bash

Why does this:

date +%d%m%Y -d "01052018 + 1 day"

error w/

date: invalid date `02062018 + 1 day'

on CentOS 7.3 in CEST? I've tried a few variations

date +%d%m%Y -d "$date 12:00 + 1 day"
date +%d%m%Y -ud "$date UTC + 1 day"

to no avail. What am I missing?

Upvotes: 2

Views: 112

Answers (1)

Inian
Inian

Reputation: 85620

GNU date does not support date format ddmmyyyy of type as you can see from Pure numbers in date strings, you need to change it of type yyyymmdd to make it work

date -d "20180501 + 1 day"

or with UTC as

date -ud "20180501 UTC + 1 day"

If your original string is from a variable and you need a work-around to make it compatible with the format above, do it using parameter expansion

rawdate="02062018"
compatDate="${rawdate:4}${rawdate:2:2}${rawdate:0:2}"

and use the variable compatDate in the date command

date -d "${compatDate} + 1 day"

Upvotes: 4

Related Questions