Sponsored Content
Full Discussion: Calculate given date - 1 day
Top Forums Shell Programming and Scripting Calculate given date - 1 day Post 302930249 by RavinderSingh13 on Thursday 1st of January 2015 05:08:20 AM
Old 01-01-2015
Quote:
Originally Posted by kmanivan82
Hi Team,
We have a requirement as follows.
If a date 20141220 as parameter to the script, then the script has to return the output as 20141219.
i.e given date - 1.
The requirement is simple. But it should satisfy leap year, the months having 31 and 30 days, the date in which day light savings changes happens, etc,.
The date has to be calculated based on the server date and time.
Can anyone help me out?
Hello kmanivan82,

Pravin's code will work if you have GNU datein your system. Following code may help you to get the previous date of any input date passed by you to script as follows.
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;print YEAR MONTH DATE}}'

Followings are some examples while running the script:
i- simply get previous day
Code:
./date_check.ksh 20201212
20201211   ####### Output

ii- To check about leap year:
Code:
./date_check.ksh 20200301
2020229     ####### Output

iii- To check date of previous month.
Code:
./date_check.ksh 20200401
2020331    ####### Output

This has been made to get the previous date only, kindly check it and let me know if this helps and you have any queries.

EDIT: Adding a non one liner form of code.
Code:
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;
                                                                                print YEAR MONTH DATE
                                                     }
                                              }'

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-01-2015 at 06:15 AM.. Reason: Added a non one liner form of code
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 10:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy