Calculate given date - 1 day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate given date - 1 day
# 8  
Old 01-05-2015
Perl
Code:
#!/usr/bin/perl
use Date::Calc qw (Add_Delta_DHMS);
$dateParam=shift;
@date1=unpack("A4 A2 A2", $dateParam);
@date2 = Add_Delta_DHMS(@date1,0,0,0,-1,0,0,0);
print "@date2 \n";

Invocation
Code:
perl dateCalc.pl 20141220

This User Gave Thanks to pravin27 For This Post:
# 9  
Old 01-06-2015
Hi RavinderSingh13,

Need a modification in the output.

I have parameterized the code that you have given as follows.

Code:
	GetDate=$1
	echo $GetDate | awk -vs1="01" -vs2="03" -vs3="12" '{if(length($0)>8){print "Wrong Input!!";exit};YEAR=substr($0,1,4);YEAR+0;MONTH=substr($0,5,2);MONTH+0;DATE=substr($0,7,2);DATE+0;split("31 29 31 30 31 30 31 31 30 31 30 31", W," ");if(DATE==s1 && MONTH==s1){YEAR=YEAR-1; MONTH=12; DATE=31; print YEAR MONTH DATE}  else if(DATE==s1 && MONTH >= s1 && MONTH <= s3){MONTH=MONTH-1;W[2]=(YEAR%4!=0)?28:29;print YEAR MONTH W[MONTH]} else {DATE=DATE-1;print YEAR MONTH DATE}}'

if i exec the script by passing the date, the script will return previous date as output.

ksh date_check.ksh 20150106
2015015


The output is displayed correct. But I need the output day should be displayed in 2 digits. I am expecting the same output to be displayed as 20150105

Output should always be
year 4 digits
month 2 digits
day 2 digits

Can you help me out?

Last edited by rbatte1; 01-07-2015 at 06:08 AM.. Reason: Added CODE tags
# 10  
Old 01-07-2015
Hello kmanivan82,

Kindly use code tags always for codes/commands/Inputs you are providing in your posts as per forum rules. Following may help you in same.
Code:
cat date_check1.ksh
echo $1 | awk -vs1="01" -vs2="03" -vs3="12" '{
                                                if(length($0)>8){
                                                                        print "Wrong Input!!";
                                                                        exit
                                                                }
                                                YEAR=substr($0,1,4);
                                                YEAR+0;
                                                MONTH=substr($0,5,2);
                                                MONTH+0;
                                                DATE=substr($0,7,2);
                                                DATE+0;
                                                split("31 29 31 30 31 30 31 31 30 31 30 31", W," ");
                                                if(DATE==s1 && MONTH==s1){
                                                                                YEAR=YEAR-1;
                                                                                MONTH=12;
                                                                                DATE=31;
                                                                                print YEAR MONTH DATE
                                                                         }
                                                else if(DATE==s1 && MONTH >= s1 && MONTH <= s3){
                                                                                MONTH=MONTH-1;
                                                                                W[2]=(YEAR%4!=0)?28:29;
                                                                                print YEAR MONTH W[MONTH]
                                                                                                }
                                                else {
                                                                                DATE=DATE-1;
                                                                                printf("%04d%02d%02d\n", YEAR, MONTH, DATE);
                                                     }
                                              }'

When we run code it will show output as follows.
Code:
./date_check1.ksh 20150106
20150105

EDIT: Adding one liner form for same.
Code:
cat date_check.ksh
echo $1 | awk -vs1="01" -vs2="03" -vs3="12" '{if(length($0)>8){print "Wrong Input!!";exit};YEAR=substr($0,1,4);YEAR+0;MONTH=substr($0,5,2);MONTH+0;DATE=substr($0,7,2);DATE+0;split("31 29 31 30 31 30 31 31 30 31 30 31", W," ");if(DATE==s1 && MONTH==s1){YEAR=YEAR-1; MONTH=12; DATE=31; print YEAR MONTH DATE}  else if(DATE==s1 && MONTH >= s1 && MONTH <= s3){MONTH=MONTH-1;W[2]=(YEAR%4!=0)?28:29;print YEAR MONTH W[MONTH]} else {DATE=DATE-1;printf("%04d%02d%02d\n", YEAR, MONTH, DATE)}}'


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-07-2015 at 12:58 AM.. Reason: added a note + added a one liner form of code
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
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. Shell Programming and Scripting

Calculate Julian date of a given date

How to get Julian date (Three digit) of a given date (Not current date)? I do not have root privilege - so can not use date -d. Assume that we have three variables year, month and date. Thx (5 Replies)
Discussion started by: Soham
5 Replies

4. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

5. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

6. UNIX for Dummies Questions & Answers

Calculate the day after bash

Hello I would like to help me create a script that: Given a date, more precisely a year, one month and one day. must calculate the day after -Input: Year, Month, Day -Output: Next day the Year-Month-Day thanks (9 Replies)
Discussion started by: karkov
9 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question