Subtract -1 from a date in Unix script | 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 !!

Subtract -1 from a date in Unix script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-13-2009
akashtcs akashtcs is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 August 2010, 7:45 AM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Subtract -1 from a date in Unix script

Hi

I have a date in a variable in Unix script and have to subtract -1 from the date.


Code:
fromdate='07/13/2009'

Now we want to subract one day from it i.e the end result should be '07/12/2009'

Any suggestions?

Thanks
Sponsored Links
    #2  
Old 11-13-2009
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
if that is a PERL script, you can use Date::Manip, or Date::Calc .


if you want that to be done from a current date, then you can do it simply with GNU date command, as


Code:
$ date --date='1 day ago'
Thu Nov 12 15:07:46 IST 2009
$ date
Fri Nov 13 15:07:53 IST 2009

Sponsored Links
    #3  
Old 11-13-2009
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 20 October 2014, 10:24 AM EDT
Posts: 2,660
Thanks: 0
Thanked 17 Times in 17 Posts
gawk

Code:
fromdate="07/13/2009"
yesterday=$( awk -v fd="$fromdate" 'BEGIN{
  m=split(fd,date,"/")
  t=mktime(date[3]" "date[1]" "date[2]" 0 0 0")
  yesterday=strftime("%d/%m/%Y",t-86400)
  print yesterday  
}' )
echo $yesterday

output

Code:
# ./shell.sh
12/07/2009

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
script for add and subtract two hours ZINGARO Shell Programming and Scripting 7 08-17-2006 08:04 AM
Subtract 2 months from the date mahekr2000 UNIX for Advanced & Expert Users 5 07-23-2006 12:01 PM
Subtract date & time in diferent rows vanand420 UNIX for Dummies Questions & Answers 1 07-10-2005 08:07 AM
Subtract two date in unix vanand Shell Programming and Scripting 1 06-18-2005 07:19 AM
How to subtract 2 hours from 'date' in shell ( /bin/sh ) script ? yairon UNIX for Dummies Questions & Answers 1 06-07-2001 03:23 PM



All times are GMT -4. The time now is 09:35 PM.