$ date -d Apr 1 2016 - 1 month +%Bgives me March, which is great - the month that comes before April is March.If I do this however:$ date -d $(date -d Apr 1 2016) - 1 month +%Bit gives me February. This is causing a bug in some more complex code I have. Why are these 2 commands showing different results?
Why are these 2 date commands giving different results?
date
You can see the problem by turning on the shell trace:+ date -d 'Apr 1 2016 - 1 month' +%BMarch++ date -d 'Apr 1 2016'+ date -d 'Fri Apr  1 00:00:00 EDT 2016 - 1 month' +%BFebruaryWhen 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 2016Your 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.