Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting date one month before today using unix. 2

Status
Not open for further replies.

RajShekar

Programmer
May 27, 2004
14
US
How to get the date 30 days before today using unix scripting? The month changes and year changes shold be considered.
 
my favorit is julian<->gregorian conversion

Code:
#-----------------------------------------------------------------------------
# Date manipulation routines - convering to Julian and back
#
# Julian Day Number from calendar date
date2julian() #  day month year
{
  typeset year=$1;  typeset month=$2;  typeset day=$3
  tmpmonth=$((12 * year + month - 3))
  tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear +     tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}

# Calendar date from Julian Day Number
julian2date() # julianday
{
  #set -x
  typeset tmpday=$(($1 - 1721119))
  typeset centuries=$(( (4 * tmpday - 1) / 146097))
  tmpday=$((tmpday + centuries - centuries/4))
  typeset year=$(( (4 * tmpday - 1) / 1461))
  tmpday=$((tmpday - (1461 * year) / 4))
  typeset month=$(( (10 * tmpday - 5) / 306))
  day=$((tmpday - (306 * month + 5) / 10))
  month=$((month + 2))
  year=$((year + month/12))
  month=$((month % 12 + 1))
  printf "%s %02d %02d" $year $month $day
}

here's how to get the previous day:

Code:
datePrevious() # $1 - year:month:day
{
    #set -x
    typeset dateI=$(echo "${1}" | sed -e "s/:/ /g");
    echo "$(julian2date $(( $(date2julian ${dateI}) - 1 )) )" | sed -e "s/ /:/g";
}

today="$(date +%Y:%M%d)"
echo "today->[${today}] yesterday->[$(datePrevious ${today})]

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Take a look at my FAQ: faq80-4800

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

Try this shell script using perl ctime and time functions

# SECONDS_IN_30DAYS contains the number of seconds in 30 days
#
SECONDS_IN_30DAYS=0
(( SECONDS_IN_30DAYS=30 * 24 * 60 * 60 ))
# use perl functions time and ctime
# time function gives today's date in seconds sine Jan 01 1970
# ctime ( seconds) converts epoch date to locale date
perl -e " use POSIX; print ctime (time() - $SECONDS_IN_30DAYS) "

Ali
 
Another way...
Code:
#!/usr/bin/ksh
Days=30
Date=$(perl -e '
  $d = time() - (86400 * $ARGV[0]);
  @f = localtime($d);
  printf "%04d%02d%02d\n", $f[5]+1900, $f[4]+1, $f[3];
  ' $Days)
echo The date $Days days ago was $Date
The date 30 days ago was 20040428
 
Another way (without perl) :
Code:
date -r$(( printf -- "-30*24*60*60+"; date +%s ) | bc)

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top