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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the end of next month date and deal with leap years. 1

Status
Not open for further replies.
Dec 12, 2007
19
US
How do I get the end of next month's date and deal with end of year and February during leap years?

Is there a UNIX command that returns this or do I have to develop some sort of script logic using the date command.

The requirements I have are, given today's date, what is the end of next month's date in the format of MM/DD/CCYY where MM is next month, DD is the last day of next month and CCYY is the century and year of next month.

Thanks in advance.
 
maybe clumsy, but here goes:

month=`date +%m -d "next month"`
year=`date+%Y -d "next month"`
for day in `cal $month $year`
do
lastday=$day
done
echo $month"/"$lastday"/"$year
 
Thanks. I found this code and it works for the end of year and leap year scenarios.

function pn_month {
typeset ym=$1 pn=$2
(( m = ym % 100 ))
(( y = ym / 100 ))
while (( pn != 0 )); do
if (( pn > 0 )); then
if (( m == 12 ))
then (( m = 1 )); (( y = y + 1 ))
else (( m = m + 1 ))
fi
(( pn = pn - 1 ))
else
if (( m == 1 ))
then (( m = 12 )); (( y = y - 1 ))
else (( m = m - 1 ))
fi
(( pn = pn + 1 ))
fi
done
printf "%s\n" $(( 100*y + m ))
}

function end_month {
typeset ym=$1 y m ld
(( y = ym / 100 ))
(( m = ym % 100 ))
for ld in $(cal $m $y); do :; done
if [ $m -lt 10 ]
then m="0${m}"
fi
echo "${m}/${ld}/${y}"
}

function begin_month {
typeset ym=$1 y m ld
(( y = ym / 100 ))
(( m = ym % 100 ))
for ld in $(cal $m $y); do :; done
if [ $m -lt 10 ]
then m="0${m}"
fi
echo "${m}/01/${y}"
}

pn_month "`date +%Y%m`" +1 | read NextMonth
end_month $NextMonth | read EndOfNextMonth
begin_month $NextMonth | read BeginOfNextMonth
 
unformated:
Code:
date -d $(date +%Y-%m -d+2month)-01-1day
Take (inner brace) the date of now + 2 months.
Use Year and month. (2010-10-)
Append -01: 2010-10-01
Substract (outer commdand) 1day.

Formatted to your needs:
Code:
date +%M/%d/%Y -d $(date +%Y-%m -d+2month)-01-1day

Source: man date
which says:
"The date string format is more complex than is easily documented here but is fully described in the info documentation."
The infopages (info date) tell you more.

don't visit my homepage:
 
Stefan's algorithm implemented in PERL, which you may find more ubiquitous than the GNU version of date:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url],[black][b]undef[/b][/black],[black][b]undef[/b][/black],[black][b]undef[/b][/black],[blue]$mon[/blue],[blue]$year[/blue],[black][b]undef[/b][/black],[black][b]undef[/b][/black],[black][b]undef[/b][/black][red])[/red] = [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] strftime [red]"[/red][purple]%m/%d/%Y[purple][b]\n[/b][/purple][/purple][red]"[/red],[black][b]localtime[/b][/black][red]([/red][maroon]mktime[/maroon][red]([/red] [fuchsia]0[/fuchsia], [fuchsia]0[/fuchsia], [fuchsia]0[/fuchsia], [fuchsia]0[/fuchsia], [blue]$mon[/blue]+[fuchsia]2[/fuchsia], [blue]$year[/blue] [red])[/red][red])[/red][red];[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top