You can see the problem by turning on the shell trace:
+ date -d 'Apr 1 2016 - 1 month' +%B
March
++ date -d 'Apr 1 2016'
+ date -d 'Fri Apr 1 00:00:00 EDT 2016 - 1 month' +%B
February
When you use the output of the inner date
command, it is at the very beginning of April, and when subtracting a month runs into the discontinuity due to EST/EDT changing:
+ date -d 'Fri Apr 1 00:00:00 EDT 2016 - 1 month'
Mon Feb 29 23:00:00 EST 2016
Your results, of course, will vary according to your local timezone settings. Turning on the trace will show the timezone (in my case, EDT
).
The reason why the results differ is that in the latter case you have given more information to date
, made its parameter more specific, i.e., the specific time of day. In the first part, that was not specified, giving date
more leeway about how to determine the date/time to display.
March
– cuonglm Feb 20 '16 at 15:04