getting previous day.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting previous day.
# 1  
Old 02-24-2010
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
# 2  
Old 02-24-2010
# 3  
Old 02-25-2010
Try this,

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

# 4  
Old 02-25-2010
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.
# 5  
Old 02-25-2010
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.
# 6  
Old 02-26-2010
MySQL

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-09-2010 at 12:42 AM..
# 7  
Old 02-26-2010
YESTERDAY=`TZ=CST+24 date +%Y%m%d`
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get previous day from UNIX AIX?

Does anybody know how to get previous day on UNIX AIX? I tried TZ=XYZ+24 date '+%y%m%d' But it doesn't understand Thanks for contribution (6 Replies)
Discussion started by: digioleg54
6 Replies

2. Shell Programming and Scripting

Get Previous day error from alertlog

Hi All, I want to get the all ORA- related errors from attached logfile for previous day. Pls help. Thanks ! Regards Kamal (7 Replies)
Discussion started by: kamauv234
7 Replies

3. Shell Programming and Scripting

View file on previous day

Hi Expert, I am new to UNIX , I am looking for a script which can show me list of file created on previous date within specified directory. Regards, Mack (9 Replies)
Discussion started by: mackjack
9 Replies

4. UNIX for Dummies Questions & Answers

List the files of previous day

I am using KSH. d=date | nawk '{ print $2,$3-1}' ls -lrt SystemOut* | nawk '/d/{print $NF}' I am trying to list the files of previous day but it doesn't seem to be working and i am also not getting an error. result is blank.Need help. (11 Replies)
Discussion started by: vagar11
11 Replies

5. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

6. Shell Programming and Scripting

Script to check if last modified day is previous day

Hi, I would like to write a script that checks if a file ('counter') was modified the previous day, if so erase its contents and write 00000000 into it. For e.g. if the file 'counter' was last modified at 11.30pm on 24th May and the script runs at 12.15am of 25th May, it should erase it's... (1 Reply)
Discussion started by: hegdepras
1 Replies

7. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

8. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

9. Shell Programming and Scripting

next/Previous business day

Hello, Can you please help me how do i get previous and next working day of the week for a given date excluding saturday and sunday. Ex: if the given date is monday, i should get friday and tuesday's date if the given date is friday, i should get thrusday and monday's date. Thanks,... (4 Replies)
Discussion started by: kotasateesh
4 Replies

10. Shell Programming and Scripting

Previous day's date in Perl?

Hi All, I want to find the previous day's date and store that in a variable, which will be usuful for further processing. Any help please. Regards, raju (4 Replies)
Discussion started by: rajus19
4 Replies
Login or Register to Ask a Question