Get the previous month from date | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Get the previous month from date

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-02-2012
abhi_123 abhi_123 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 13 October 2014, 10:03 PM EDT
Posts: 42
Thanks: 5
Thanked 0 Times in 0 Posts
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 11:13 AM.. Reason: Code tags
Sponsored Links
    #2  
Old 12-02-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 21 November 2014, 11:17 AM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 235
Thanked 1,210 Times in 1,136 Posts

Code:
date -d'-1 month' '+%Y%m'
201211


Code:
uname
Linux
echo $SHELL
/bin/bash

Sponsored Links
    #3  
Old 12-02-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
Quote:
Originally Posted by abhi_123 View Post
how do i get the previous month value(ie: subtract 1 from date)
Search on "datecalc" in this forum.
    #4  
Old 12-02-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 November 2014, 5:29 PM EST
Location: San Jose, CA, USA
Posts: 5,082
Thanks: 195
Thanked 1,703 Times in 1,446 Posts
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" $dt

until we cross the y10K date boundary.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 11:38 AM
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 09:11 AM
Pass the first date and last date of previous month vigdmab Shell Programming and Scripting 2 03-05-2010 12:19 PM
How to get the previous month by using `date` suneelj UNIX for Dummies Questions & Answers 7 02-05-2009 05:58 PM
Help, I need to get the last date of previous month sirrtuan Shell Programming and Scripting 11 10-14-2008 06:59 AM



All times are GMT -4. The time now is 06:30 PM.