![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help, I need to get the last date of previous month | sirrtuan | Shell Programming and Scripting | 11 | 10-14-2008 02:59 AM |
| Use date command to find last month | Cbish68 | Shell Programming and Scripting | 5 | 08-10-2007 07:32 AM |
| find out month from a date | rudoraj | UNIX for Dummies Questions & Answers | 5 | 07-03-2007 05:21 AM |
| Formatting Date (adding a month) | devid | UNIX for Dummies Questions & Answers | 4 | 01-18-2006 07:31 AM |
| how to get month last date in unix | rajan_ka1 | Shell Programming and Scripting | 12 | 10-04-2005 04:20 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
last month end date
Hi,
How to get the lastmonth's end date? (have current date) through s shell script. I need to get it and pass it to a procedure. Please advice. Thanks in advance. |
| Forum Sponsor | ||
|
|
|
#3
|
|||
|
|||
|
For future reference if someone reads this:
try cal Code:
#!/bin/ksh
printf "%d %d" $(date "+%Y %m") | read year month
let month=$month-1
if [[ $month -eq 0 ]] ; then
let year=$year-1
let month=12
fi
cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' | read day
printf "%d/%02d/%02d\n" $year $month $day
Last edited by jim mcnamara; 03-21-2008 at 10:01 AM. Reason: changed month=1 to month=12 |
|
#4
|
||||
|
||||
|
Code:
if [[ $month -eq 0 ]] ; then let year=$year-1 let month=1 fi Code:
if [[ $month -eq 0 ]] ; then let year=$year-1 let month=12 fi |
|
#5
|
|||
|
|||
|
Perl-based approach to deriving backwards in time...I haven't quite bothered to try going back to the future as of yet. But it also handles Leap Years...
Code:
$ pl_end_of_last_month_0=`perl -e '\ > $y= time - (86400 * (localtime(time))[3]); \ > printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ` $ echo $pl_end_of_last_month_0 20070831 === Code:
$ # Today...
$ pl_today_0=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 0 `
$ echo $pl_today_0
20070912
===
$ # Today minus 1... (um, yesterday...?)
$ pl_today_1=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 1 `
$ echo $pl_today_1
20070911
===
$ # Today minus a defined number...
$ my_number=3
$ pl_today_mynumber=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ${my_number} `
20070909
===
|
|
#6
|
|||
|
|||
|
Quote:
|
|
#7
|
|||
|
|||
|
And perl/python/C is better solution.
|
|||
| Google The UNIX and Linux Forums |