Friday, 8 February 2013

Finding the first day and last day of the last month using date command in linux

First day of Last Month = echo $(date --date "-1 month" +%Y-%m-01)
Last day of Last Month = echo $(date --date "-`date +%d` days -0 month" +%Y-%m-%d)


First day of Current Month = echo $(date --date "-0 month" +%Y-%m-01)
Last day of Current Month = echo $(date --date "-`date +%d` days +1 month" +%Y-%m-%d)

First day of Next Month = echo $(date --date "+1 month" +%Y-%m-01)
Last day of Next Month = echo $(date --date "-`date +%d` days +2 month" +%Y-%m-%d)

1 comment:

  1. Very helpful! :)

    Just had to change the date format to match my locale :P

    ReplyDelete