Get Previous date of a past date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Previous date of a past date
# 1  
Old 10-31-2011
Get Previous date of a past date

Hi all,
I have a variable where it has a past date value in the format YYYY-MM-DD

eg->
pcontromModate="2008-11-31"

How can i get the date 1 day before the pcontromModate.

The required date is 2008-11-30. ..Plz reply since its an urgent one

Last edited by morbid_angel; 10-31-2011 at 03:26 PM..
# 2  
Old 10-31-2011
Please look here:

https://www.unix.com/answers-frequent...rithmetic.html

I think I remember you asking this question before. Here is the exact shell script, maybe this will help. This is a slight modification of one of the solution from the above post. Credit goes to the original author of the code, I just made slight modification.

Code:
#!/bin/sh
pcontromModate="2008-11-31"
YEAR=`echo $pcontromModate|cut -d "-" -f 1`
MONTH=`echo $pcontromModate|cut -d "-" -f 2`
DAY=`echo $pcontromModate|cut -d "-" -f 3`
DAY=`expr "$DAY" - 1`
case "$DAY" in
        0)
           MONTH=`expr "$MONTH" - 1`
                case "$MONTH" in
                        0)
                           MONTH=12
                           YEAR=`expr "$YEAR" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
echo "Yesterday was: $MONTH-$DAY-$YEAR"

here is the output from the above:

Code:
# /tmp/45.sh
Yesterday was: 11-30-2008


Last edited by dude2cool; 10-31-2011 at 03:47 PM..
# 3  
Old 10-31-2011
What will be the output if pcontromModate is "2008-05-05" .Will it print in date format
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

2. Shell Programming and Scripting

Script to get previous date for the given input date..

hi all, need a script or command to get the previous date for the given input date... like in my script i will pass date as input parameter like 2014-12-01 and i want the output as previous date.. ie.. 2014-11-30 (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Date - incorrect results for previous date

Hello: I am bit puzzled with what I could be doing wrong and any help is appreciated. I have a date in YYYMMDD format and I need to find the previous date. Based on the input on this forum, I have come up with the following. It seems to work for all except the following. Here I am passing date... (3 Replies)
Discussion started by: wincrazy
3 Replies

4. Shell Programming and Scripting

Seven days past date from current date

hi all.. i want 2 know how 2 find 7days past date from current date.. when i used set datetime = `date '+%m%d%y'` i got 060613.. i just want to know hw to get 053013.. i tried using date functions but couldnt get it :( i use c shell and there is no chance that i can change that ..... (3 Replies)
Discussion started by: Rahul619
3 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

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

7. Shell Programming and Scripting

How to find a date which is 7 days past when given current date

hii all. I have to get the date of the 7th day past from the current date. if i give the current date as sep 3 then i must get the date as 27th of august. can we get the values from the "cal" command. cal | awk '{print $2}' will this type of command work. actually my need is if today is... (17 Replies)
Discussion started by: ladtony
17 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies

10. Shell Programming and Scripting

Specify a previous date as start date in shell script

Hi, I am trying to modify a script which accepts date in format dd/mm/yy. I am trying to modify the script so that it retrieves the date that was 15 days earlier from today as start date. Eg.if today is 05/09/2006, the script should retrieve 21/08/2006 as start date. Is there any script/code to... (2 Replies)
Discussion started by: ritzwan0
2 Replies
Login or Register to Ask a Question