![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| 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 05:59 AM |
| Use date command to find last month | Cbish68 | Shell Programming and Scripting | 5 | 08-10-2007 10:32 AM |
| find out month from a date | rudoraj | UNIX for Dummies Questions & Answers | 5 | 07-03-2007 08:21 AM |
| Formatting Date (adding a month) | devid | UNIX for Dummies Questions & Answers | 4 | 01-18-2006 10:31 AM |
| how to get month last date in unix | rajan_ka1 | Shell Programming and Scripting | 12 | 10-04-2005 07:20 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 01:01 PM.. Reason: changed month=1 to month=12 |
|
||||
|
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
===
|
|
||||
|
Quote:
|
|
||||
|
And perl/python/C is better solution.
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|