-2

Below is my output but I want to get output like Apr 30 23:59:59. Please suggest

date -d "$(date +%Y-%m-01) -1 day"

Output: Tue Apr 30 00:00:00 UCT 2019

I want output: Tue Apr 30 23:59:59 UCT 2019

barbsan
  • 3,418
  • 11
  • 21
  • 28

1 Answers1

0

You need to pass another +format to date and replace the time with the 23:59:59. Something like this:

date -d "$(date +%Y-%m-01) -1 day" +"%a %b %d 23:59:59 %Z %Y"

And the output will be:

Tue Apr 30 23:59:59 -03 2019

Update: to have the last day of the previous month using the format YYYYMMDD, just change the last format string to '%Y%m%d'. Here is the full command:

date -d "$(date +%Y-%m-01) -1 day" +"%Y%m%d"

And here is the output:

20190430
accdias
  • 5,160
  • 3
  • 19
  • 31