|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Get the previous month from date
Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. Code:
dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: Code:
dt=201211 Please help.
Last edited by Scott; 12-02-2012 at 10:13 AM.. Reason: Code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Code:
date -d'-1 month' '+%Y%m' 201211 Code:
uname Linux echo $SHELL /bin/bash |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Search on "datecalc" in this forum.
|
|
#4
|
|||
|
|||
|
If you are on a system where date doesn't have a -d option, the following will work in any POSIX conforming shell: Code:
#!/bin/ksh
dt=$(date +%Y%m)
dt=$((dt - 1))
if [ ${dt#????} -eq 0 ]
then dt=$((${dt%??} - 1))12
fi
printf "dt has been set to %s\n" $dtuntil we cross the y10K date boundary. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| date command - getting previous month | ab_2010 | UNIX for Dummies Questions & Answers | 1 | 06-15-2011 10:38 AM |
| print previous month (current month minus 1) with Solaris date and ksh | slashdotweenie | UNIX for Dummies Questions & Answers | 7 | 05-14-2010 08:11 AM |
| Pass the first date and last date of previous month | vigdmab | Shell Programming and Scripting | 2 | 03-05-2010 11:19 AM |
| How to get the previous month by using `date` | suneelj | UNIX for Dummies Questions & Answers | 7 | 02-05-2009 04:58 PM |
| Help, I need to get the last date of previous month | sirrtuan | Shell Programming and Scripting | 11 | 10-14-2008 05:59 AM |
|
|