Date command is not working properly

Hi,

in my script, i take the last month by

a=$(date --date '1 month ago' +%Y%m)

i expect that it give me in this month "March" as result 201402,
but linux gave me 201403.

IMPe@ABC123:> ~/date --date '1 month ago' +%Y%m
201403

i'm reasonably confused. Any idea?

Thanks in advance,
IMPe

PS.:
System
Linux ABC123 3.11.10-7-desktop #1 SMP PREEMPT Mon Feb 3 09:41:24 UTC 2014 (750023e) x86_64 x86_64 x86_64 GNU/Linux

GNU bash, Version 4.2.45(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.

Hi, try:

date --date="$(date +%Y%m15) 1 month ago" +%Y%m

This behavior occurs, because februari 30 does not exist. So it is best to use the middle of the month to calculate the previous month..

--
Interestingly in recent ksh93 this seems to work alright:

$ printf "%(%Y%m)T\n" "last month"
201402
2 Likes

Thanks a lot Scrutinizer! I thought that this command i used is working exactly -
unfortunatelly it doesn't.
Thank you,
IMPe