Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last day of a month 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
with the date command on a linux I can obtain last day of the current month:

$ date +%e -d "-$(date +%d) day"
30

How to modify the command to get with the command any month's last day?

I tried this but it return wrong value...

$ date +%e -d "02/02/2016 -$(date +%d -d "02/02/2016") day"
31

 

I get some results with this:

Code:
$ date +%e -d "+1 month -$(date +%d) days"
31
$ date +%e -d "02/02/2016 +1 month -$(date +%d -d"02/02/2016") days"
29
$ date +%e -d "02/02/2015 +1 month -$(date +%d -d"02/02/2015") days"
28
$ date +%e -d "02/02/2014 +1 month -$(date +%d -d"02/02/2014") days"
28
$ date +%e -d "02/02/2013 +1 month -$(date +%d -d"02/02/2013") days"
28
$ date +%e -d "02/02/2012 +1 month -$(date +%d -d"02/02/2012") days"
29
$ date +%e -d "07/02/2012 +1 month -$(date +%d -d"07/02/2012") days"
31
$ date +%e -d "08/02/2012 +1 month -$(date +%d -d"08/02/2012") days"
31
$ date +%e -d "09/02/2012 +1 month -$(date +%d -d"09/02/2012") days"
30
$ date +%e -d "06/02/2012 +1 month -$(date +%d -d"06/02/2012") days"
30
$
 
Hi

Kind of cheating, but may I suggest an alternative ?
Code:
[blue]master #[/blue] cal | awk 'NF{l=$NF}END{print l}'
31

[blue]master #[/blue] cal 2 2015 | awk 'NF{l=$NF}END{print l}'
28

Regarding your [tt]date[/tt]-based solution, I would also calculate that way.


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top