Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-24-2010
Registered User
 

Join Date: Jul 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
getting previous day.

Hi,
i am developing a script for my project. In my script i need to check some conditions based upon some values. in that one value is the previous date. with the previous date i need to check and process some values. I have a function with me retrieve the yesterdays date.

But now i have a scenario that i need to check with the yesteday day alone. That is say for example today is Thu,i need to check one day before that is with Wed. i am not sure how to retrieve this Thu-1.

I am trying `date %a -1` but its not working.

Please help me in this.

Thanks,
Raju
Sponsored Links
    #2  
Old 02-24-2010
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,849
Thanks: 3
Thanked 44 Times in 43 Posts
http://www.unix.com/answers-frequent...rithmetic.html
Sponsored Links
    #3  
Old 02-25-2010
Nila's Avatar
Registered User
 

Join Date: Feb 2010
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Try this,


Code:

date -d "1 day ago" | cut -d ' ' -f 1

    #4  
Old 02-25-2010
vivekraj's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
You can try this,

date +%a -d "1 day ago"

This will give the yesterday day name in three letter format.For ex,Wednesday as Wed.If you want full day name,use %A instead of %a.
Sponsored Links
    #5  
Old 02-25-2010
kshji's Avatar
Registered User
 

Join Date: Jun 2009
Location: Finland
Posts: 499
Thanks: 0
Thanked 36 Times in 36 Posts
ksh93 include printf extension. Gnu date command include also epoc + calculation extensions. Some bsd date include -r option.
Ex. using ksh93

Code:
   printf "%T" now

More example to use printf and date calculation.

Or using gnu date command like

Code:
#!/someposixsh
epoc=$(date -u +%s)

echo $epoc
(( yesterday=epoc-86400 ))

datestr=$(date -d "1970-01-01  $epoc seconds"  )
echo  $datestr
datestr=$(date -d "1970-01-01  $epoc seconds" '+%Y-%m-%d %H:%M:%S' )
echo  $datestr
echo "yesterday:"
date -d "1970-01-01  $yesterday seconds" '+%Y-%m-%d %H:%M:%S'

If using date, read your man date.
Sponsored Links
    #6  
Old 02-26-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 142
Thanks: 5
Thanked 17 Times in 6 Posts
Thumbs up

The following example used for current date from ago

Code:

        #date
        Tue Feb 23 14:32:14 IST 2010

        #date -d "1 day ago"
        Mon Feb 22 14:32:18 IST 2010

        #date -d "1 week ago"
        Tue Feb 16 14:32:24 IST 2010

        #date -d "1 month ago"
        Sat Jan 23 14:32:29 IST 2010

         #date -d "1 year ago"
        Mon Feb 23 14:32:35 IST 2009

The following example used for current date after

Code:
        #date
        Tue Feb 23 14:35:00 IST 2010

        #date -d "+1 day"
        Wed Feb 24 14:35:05 IST 2010

        #date -d "+1 week"
        Tue Mar  2 14:35:09 IST 2010

        #date -d "+1 month"
        Tue Mar 23 14:35:14 IST 2010

        #date -d "+1 year"
        Wed Feb 23 14:35:21 IST 2011

for more information read the date man page.

Last edited by ungalnanban; 03-08-2010 at 11:42 PM..
Sponsored Links
    #7  
Old 02-26-2010
Registered User
 

Join Date: Nov 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
YESTERDAY=`TZ=CST+24 date +%Y%m%d`
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
Get Previous Date dinjo_jo Shell Programming and Scripting 5 11-04-2009 10:11 PM
previous posts james6 Forum Support Area for Unregistered Users & Account Problems 2 06-17-2008 06:31 AM
Previous command new2ss HP-UX 5 02-14-2008 05:06 AM
Get the Previous date Nayanajith Shell Programming and Scripting 2 06-29-2006 01:39 AM
Value of previous row using awk videsh77 Shell Programming and Scripting 2 02-18-2005 12:23 PM



All times are GMT -4. The time now is 12:50 AM.