Calculate given date - 1 day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate given date - 1 day
# 1  
Old 01-01-2015
Calculate given date - 1 day

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?
# 2  
Old 01-01-2015
try this
Code:
date -d '20141220 1 day ago'  '+%Y%m%d'

# 3  
Old 01-01-2015
That depends on what your system - which you fail to mention - provides. Above is an example of GNU date, which is not avaiable on every system. (Free)BSD provides
Code:
date -v-1d -jf "%Y%m%d" 20141220 +"%Y%m%d"
20141219

Recent bash has the printf "%(datefmt)T" builtin, but that needs epoch seconds, which may be difficult to get at.
# 4  
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
# 5  
Old 01-01-2015
Try this
Code:
date -d'yesterday' +%Y%m%d

# 6  
Old 01-02-2015
If you don't have GNU date, then another alternative is perl if you have that. From your input date, we need to split it up the values and feed them in:-
Code:
#!/bin/ksh

input_date=$1

in_yyyy="${input_date%????}"     # Remove last four characters
in_dd="${input_date#??????}"     # Remove lead four characters
in_mm="${input_date#????}"       # Remove lead four characters
in_mm="${in_mm%??}"              # Remove last two characters from previously split value

((temp_mm=$in_mm-1))             # Reduce month by one as epoch counts from 1/1/1970 as zero

# Get time in seconds since epoch
perl -e "use Time::Local; print timelocal(0,0,0,$in_dd,$temp_mm,$in_yyyy), ;" | read in_seconds

# Go back one day in seconds
((out_seconds=$in_seconds-86400))

# Display answer in required format
perl -e 'use POSIX qw(strftime);print scalar(strftime "%Y%m%d", localtime $ARGV[0]), "\n";' $out_seconds

I hope that this meets the brief you have given us. There may be smarter ways to read & split the input date, so I'm open to suggestions.

Obviously some validity checking on the input would be wise else you will get various horrible messages from the perl

I've put it in a script called 254222 (thread number) and tested it as shown:-
Code:
$ 254222 20141220
20141219

$ 254222 20140301
20140228

$ 254222 20160301
20160229

$


I hope that this helps. I had fun with the challenge and I'm pretty sure I'm going to need it soon myself too, so thanks for the query Smilie


Robin
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 01-04-2015
Thank you All.

The code does not work in our system. We are using AIX 6.1

Code:
date -d '20141220 1 day ago'  '+%Y%m%d'


Right now, I am using the code given by RavinderSingh13. Yet to try out the code given by rbatte1.

Is there anyway this funciton can be implemented simply using perl, Since Perl has the date datatype?
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