How to derive the last Sunday's date when Current date value is passed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to derive the last Sunday's date when Current date value is passed
# 1  
Old 03-08-2012
How to derive the last Sunday's date when Current date value is passed

Hi All,

I have a requirement in my project where a batch runs on any day of a week. The input files will land to the server every Sunday. I need to read these files based on the current BUS_DAY (which falls any day of the week).

For e.g : if the BUS_DAY is 20120308, we need to derive the filedate as 20120304. I am trying the same as below,

Code:
export DAYS_AGO=`date +%w`
echo $DAYS_AGO
export SUB_DATE=`date -d "-$DAYS_AGO day" "+%Y%m%d"`
echo "P_SUB_DATE=$SUB_DATE" >>$COMMON_TMP/CPMGDynamicDateFile.txt

Can you pls let me know whether this will work for dates like 20120302(first days of the month). Any better approach will be much appreciated.

Thanks
Freddie

Last edited by Corona688; 03-08-2012 at 05:36 PM..
# 2  
Old 03-08-2012
Seems to work.

Code:
$ date -d "20120308 - 4 days" +%Y%m%d
20120304

$

It's important to remember that this is a feature of GNU date only, and perhaps only newer versions of GNU date. Systems other than Linux are very unlikely to have it.
# 3  
Old 03-08-2012
Thanks for your reply Corona.

I am using LINUX & i hope this command will work fine without any issues.

My only concern is whether this command will derive the correct value for the dates like 20120302. In this case, the value derived should be 20120226. Since i dont have privileges to change the system date, I am unable to test it.

Can you Pls help me/

Thanks
Freddie
# 4  
Old 03-08-2012
You dont need to change the system date just use:
Code:
$ date -d "20120302 - 5 days" +%Y%m%d
20120226

The above just prints the date 5 days prior to Mar 02 2012, it dosn't change the system date.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

4. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

5. Shell Programming and Scripting

Derive date and its Volume number

Hi All, I have a list of date number (YYYYMMDD) and its own number in a file. Here is the example 20170320 D100001 D100002 D100003 20170321 D200001 D200002 D200004 D200005 20170322 D200005 D200006 D200007 (4 Replies)
Discussion started by: ckwan123
4 Replies

6. Shell Programming and Scripting

Derive the Year value from the date value

Hi All, I have two dates: PREVIOUS_DAY and CURRENT_DAY. I need to test the these two values years are same or not. PREVIOUS_DAY like '%y%m%d' i.e values like 111010, 111011, 111012 etc. For CURRENT_YEAR:'date +%Y' use this command. How can derive the year value from PREVIOUS_DAY and... (1 Reply)
Discussion started by: pdathu
1 Replies

7. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

Last sunday of current date

Hi Please help me with this problem i need to find the date of last week sunday from the current given date (12 Replies)
Discussion started by: aishsimplesweet
12 Replies

9. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

10. Shell Programming and Scripting

Derive date from a calendar

I'm using AutoSys as scheduler for my application. I maintain a calendar in AutoSys which specifies when a job should run. A unix shell scripts runs on the days spceified by calendar and processes incoming files. These incoming files contain dates embedded in the filenames. My job needs to... (2 Replies)
Discussion started by: autouser123
2 Replies
Login or Register to Ask a Question