• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Solved the date command. A given date +21 days/3 weeks

k.jacker

Well-Known Member

Thanks: 105
Messages: 250

#1
Hei folks,

I've been trying to use date to add 21 days (or 3 weeks) to a given date, e.g.:
(for everybody's convenience here in US and european output format)
Code:
% date -v15d -v1m "+Europe %d.%m.%Y%nUS %D"
Europe 15.01.2017
US 01/15/17
So what I do for now, is adding +1 month and subtracting -8 days.
That gives me approximately the result I'm looking for, but it's not exact as you can imagine.
(Just adding +21 days does not work, because days are just counted like 29, 30, 31, 1, 2, 3...)
Code:
% date -v15d -v1m -v+1m -v-8d "+Europe %d.%m.%Y%nUS %D"
Europe 07.02.2017
US 02/07/17
Any ideas on what arguments I could use with date for en exact result?

This is going to be part of an sh script, so calculations/variables using sh are wellcome :)

Thanks!
 

ShelLuser

Daemon

Thanks: 802
Messages: 2,010

#3
Uhm, just adding 21 days should work just fine, why do you think it doesn't? Date actually keeps up with the correct date, you make it sound as if it will always count to 31 at the end of a month but I can rest assure you that it doesn't:

Code:
omicron:/home/peter $ date -v-8m -v-12d +%d%m%y
010317
omicron:/home/peter $ date -v-8m -v-13d +%d%m%y
280217
And:

Code:
omicron:/home/peter $ date +%d%m%y
131117
omicron:/home/peter $ date -v-13d +%d%m%y
311017
So why wouldn't it work to (edit): subtract add 21 days?
 

k.jacker

Well-Known Member

Thanks: 105
Messages: 250

#4
Hei Shelluser,

now I got it. It had arguments in the wrong order. Manpage says order matters, but I couldn't figure out how, so I finaly thought it doesn't work.

Making date show the date 15.01.2017 and adding 21 days to that in two ways... one works, the other doesn't.

works...
Code:
% date -v15d -v1m -v+21d "+Europe %d.%m.%Y%nUS %D"
Europe 05.02.2017
US 02/05/17
not working...
Code:
% date -v15d -v+21d -v1m "+Europe %d.%m.%Y%nUS %D"                                                 
Europe 06.01.2017
US 01/06/17
Thank you for helping me figure it out :)