delete three days from date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete three days from date
# 1  
Old 06-16-2009
delete three days from date

i want to delete three days from system date ... date -3
# 2  
Old 06-16-2009
search with string "datecalc" in the forum...one of our forum admin has written a script long ago...
# 3  
Old 06-16-2009
use the command :-
for days use
get_date d 3

for years use
get_date y 3

for months use
get_date m 3

where the code is stored in file named as "get_date" and it is executable "rwx------"

So copy the code and put it in a file called get_date and make it executable.


Code:
code:-
#!/bin/sh
# @(#)get_date.T        1.1 Copyright 1993, Motorola Inc.
#       (This is a joint Tandem/SPARC file)
#
#  This script is used to return a date.  The date returned
#  is adjusted for a "past" time.  The adjustment for date
#  may be made on either the year (y), month (m), or day (d)
#  fields. NOTE: date field only except lower case letters(ymd)
#
#  Note: Output format is YYMMDD order, no current provisioning
#        is made for different output requirements
#
#  Syntax: get_date <date_field> <date_adjustment>
#               date_field: Y = year; m = month; d = day
#          date_adjustment: integer value applied to date field
#
# 1st Sept 1997 olearya Updated script to accept a date in format
#                       yymmdd and to add and subtract one day
#                       from that. The new options are "sb" and "sf"
#
##################################################
mon=`date '+%m'`
day=`date '+%d'`
yr=`date '+%Y'`

#
# Go Backward in time.
#

##################################################
# Adjust year routine, parameter 1 is the value used for ajustment.
##################################################
adj_yr()
{
yr=`expr $yr - $1`

if [ $yr -lt 0 ]
then
  yr=`expr $yr + 100`
fi

if [ $yr -lt 0 ]
then
  echo "$0:Error: invalid value for year adjustment"
  exit 1
fi

if [ $yr -le 9 ]
then
   yr="0$yr"
fi
}
##################################################
# Adjust month routine, parameter 1 is the value used for ajustment.
##################################################
adj_mon()
{
mon=`expr $mon - $1`

while [ $mon -lt 1 ]
do
  adj_yr 1
  mon=`expr $mon + 12`
done

if [ $mon -le 9 ]
then
   mon="0$mon"
fi
}
##################################################
# Adjust day routine, parameter 1 is the value used for ajustment.
##################################################
adj_day()
{
day=`expr $day - $1`

while [ $day -lt 1 ]
do
  adj_mon 1
  if [ $mon -eq 12 -o $mon -eq 10 -o \
       $mon -eq 8 -o $mon -eq 7 -o $mon -eq 5 -o $mon -eq 3 -o $mon -eq 1 ]
  then
     day=`expr $day + 31`
  else
     if [ $mon -eq 2 ]
     then
        leap_yr=`expr $yr % 4`
        if [ $leap_yr -eq 0 ]
        then
          day=`expr $day + 29`
        else
          day=`expr $day + 28`
        fi
     else
        day=`expr $day + 30`;
     fi
  fi
done

if [ $day -le 9 ]
then
   day="0$day"
fi
}


#
# Go forward in time.
#
##################################################
# Adjust year routine, parameter 1 is the value used for ajustment.
##################################################
adj_yr_fwd()
{
yr=`expr $yr + $1`

if [ $yr -gt 99 ]
then
  yr=`expr $yr - 100`
fi

if [ $yr -lt 0 ]
then
  echo "$0:Error: invalid value for year adjustment"
  exit 1
fi

if [ $yr -le 9 ]
then
   yr="0$yr"
fi
}
##################################################
# Adjust month routine, parameter 1 is the value used for ajustment.
##################################################
adj_mon_fwd()
{
mon=`expr $mon + $1`

while [ $mon -gt 12 ]
do
  adj_yr_fwd 1
  mon=`expr $mon - 12`
done

if [ $mon -le 9 ]
then
   mon="0$mon"
fi
}
##################################################
# Adjust day routine, parameter 1 is the value used for adjustment.
##################################################
adj_day_fwd()
{
day=`expr $day + $1`
leap_yr=`expr $yr % 4`

case $mon in
  2|02)
    if [ $leap_yr -ne 0 ] && [ $day -gt 28 ]
    then
       adj_mon_fwd 1
       day=1
    fi;;
  1|01|3|03|5|05|7|07|8|08|10|12)
    if [ $day -gt 31 ]
    then
       adj_mon_fwd 1
       day=1
    fi;;
  4|04|6|06|9|09|11)
    if [ $day -gt 30 ]
    then
       adj_mon_fwd 1
       day=1
    fi;;
esac

if [ $day -le 9 ]
then
   day="0$day"
fi
}
##################################################
# execution body of get_date
##################################################

if [ $# -ne 2 ]
then
  echo "Error: `basename $0` <date_field> <date_adjustment>"
  echo "               date_field: Y = year; m = month; d = day"
  echo "               date_field: sb = date supplied, one day back"
  echo "               date_field: sf = date supplied, one day forward"
  echo "          date_adjustment: integer value applied to date field"
  echo "          date_adjustment: with options sb,sf is date in format yymmdd"
  echo "          eg get_date d 1        Go back one day from current date."
  echo "          eg get_date sf 970221  Go forward one day from given date."
  exit 1;
fi

case $1 in
  "y") adj_yr $2;;
  "m") adj_mon $2;;
  "d") adj_day $2;;
  "sb") yr=`echo $2 | cut -c1,2`; mon=`echo $2 | cut -c3,4`;day=`echo $2 | cut -c5,6`; adj_day 1;;
  "sf") yr=`echo $2 | cut -c1,2`; mon=`echo $2 | cut -c3,4`;day=`echo $2 | cut -c5,6`; adj_day_fwd 1;;
  *) echo "Error: `basename $0` <date_field> <date_adjustment>"
     echo "               date_field: y = year; m = month; d = day (lower case only)"
     echo "               date_field: sb = date supplied, one day back"
     echo "               date_field: sf = date supplied, one day forward"
     echo "          date_adjustment: integer value applied to date field"
     echo "          date_adjustment: with options sb,sf is date in format yymmdd"
     echo "          eg get_date d 1        Go back one day from current date."
     echo "          eg get_date sf 970221  Go forward one day from given date."
     exit 1;;
esac

echo $yr$mon$day
exit 0

# 4  
Old 06-16-2009
try this

[root@Reddyraja tmp]# date -d "3 days ago"
Sat Jun 13 19:04:33 IST 2009

[root@Reddyraja tmp]# date -d "3 days"
Fri Jun 19 19:05:50 IST 2009

[root@Reddyraja tmp]# date -d "3 seconds ago"
Tue Jun 16 19:04:16 IST 2009

[root@Reddyraja tmp]# date -d "3 years ago"
Fri Jun 16 19:06:41 IST 2006

[root@Reddyraja tmp]# date -d "3 years "
Sat Jun 16 19:06:45 IST 2012

for more information

info date
# 5  
Old 06-16-2009
Quote:
Originally Posted by reddysiva
try this

[root@Reddyraja tmp]# date -d "3 days ago"
Sat Jun 13 19:04:33 IST 2009

[root@Reddyraja tmp]# date -d "3 days"
Fri Jun 19 19:05:50 IST 2009

[root@Reddyraja tmp]# date -d "3 seconds ago"
Tue Jun 16 19:04:16 IST 2009

[root@Reddyraja tmp]# date -d "3 years ago"
Fri Jun 16 19:06:41 IST 2006

[root@Reddyraja tmp]# date -d "3 years "
Sat Jun 16 19:06:45 IST 2012

for more information

info date

These options are not exist in Solaris ...bad options.
BR
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Beginners Questions & Answers

Subscribers with Date 90 days older than current date

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8) -lt... (1 Reply)
Discussion started by: dia
1 Replies

3. Shell Programming and Scripting

UNIX date fuction - how to deduct days from today's date

Hi, One of my Unix scripts needs to look for files coming in on Fridays. This script runs on Mondays. $date +"%y%m%d" will give me today's date. How can I get previous Friday's date.. can I do "today's date minus 3 days" to get Friday's date? If not, then any other way?? Name of the files is... (4 Replies)
Discussion started by: juzz4fun
4 Replies

4. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

5. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

6. Shell Programming and Scripting

How to Get 60 days Old date from current date in KSH script

Hi i am writing a cron job. so for it i need the 60 days old date form current date in variable. Like today date is 27 jan 2011 then output value will be stote in variable in formet Nov 27. i am using EST date, and tried lot of solution and see lot of post but it did not helpful for me. so... (3 Replies)
Discussion started by: Himanshu_soni
3 Replies

7. Shell Programming and Scripting

Date after 5 days from current date in YYYYMMDD format

Hello Experts, How do i get date after 5 days from current date in YYYYMMDD format? How do you compare date in YYYYMMDD format? Thanks (8 Replies)
Discussion started by: needyourhelp10
8 Replies

8. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies

9. Shell Programming and Scripting

date for two days or 3 days ago

i need a script that can tell me the date 2 days ago or 3 days ago. please help (7 Replies)
Discussion started by: tomjones
7 Replies

10. 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
Login or Register to Ask a Question