Skip to content

Instantly share code, notes, and snippets.

@sursir
Last active March 20, 2019 06:40
Show Gist options
  • Save sursir/a4e6d5dc1a4ec8e08a305e18fa9bb35e to your computer and use it in GitHub Desktop.
Save sursir/a4e6d5dc1a4ec8e08a305e18fa9bb35e to your computer and use it in GitHub Desktop.
shell date last month first day of last month last day of last month
CT='2019-02-31' # init date **When LAST DAY (end of month)**
date -d "$CT" +%Y-%m-%d
date -d "$CT last month" +%Y-%m-%d # terrible last month
# last month day 1
date -d "$(date -d "$CT" +%Y-%m-01) last month" +%Y-%m-%d #fixed
# -2 month
date -d "$(date -d "$CT" +%Y-%m-01) -2 month" +%Y-%m-%d
# -3 month
date -d "$(date -d "$CT" +%Y-%m-01) -3 month" +%Y.%m
# last day of last month
date -d "$(date -d "$(date -d "$CT" +%Y-%m-01) last day")" +%Y-%m-%d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment