Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-29-2010
Registered User
 

Join Date: May 2010
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
Date time calculation

hello guys,
I had been to many forums and many topics in this site as well for my question but did not get any solution.
My question is how i can get y'day date with time stamp
today is 20100729103819 and i am looking for output as 20100728103819.
in simple words as we do in oracle sysdate-1 (with timestamp).

Thanks a lot!
Sponsored Links
    #2  
Old 07-29-2010
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts
How about this?
Sponsored Links
    #3  
Old 07-29-2010
The Underground
 

Join Date: Sep 2009
Location: ./India/Mumbai
Posts: 889
Thanks: 28
Thanked 179 Times in 174 Posts
Hi,


Code:
 date --d "1 day ago" +%Y%m%d%H%M%S

    #4  
Old 07-29-2010
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts
Quote:
Originally Posted by pravin27 View Post
Hi,


Code:
 date --d "1 day ago" +%Y%m%d%H%M%S

This is GNUism - may not be applicable.
Sponsored Links
    #5  
Old 07-29-2010
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Computer Geek
 

Join Date: Jul 2008
Location: Frederick, MD
Posts: 748
Thanks: 4
Thanked 10 Times in 9 Posts
For non GNU:

Code:
date '+%m %d %Y' |  
{  
read MONTH DAY YEAR 
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"  
}

or


Code:
OFFSET=${1:-1}

case $OFFSET in
  *[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

day=$((day - OFFSET))
if (( day <= 0 )) ;then
  month=$((month - 1))
  if (( month == 0 )) ;then
    year=$((year - 1))
    month=12
  fi
  set -A days `cal $month $year`
  xday=${days[$(( ${#days[*]}-1 ))]}
  day=$((xday + day))
fi

echo $day

EDIT: My BAD, I didnt see the format you wanted, you will have to modify a little to get your format.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Time stamp calculation maverick_here Shell Programming and Scripting 6 05-17-2009 10:59 PM
Time Calculation bpfoster76 UNIX for Dummies Questions & Answers 5 02-03-2009 08:58 AM
Date Calculation subhendu81 Shell Programming and Scripting 2 09-30-2008 01:34 AM
Time difference calculation Segwar Shell Programming and Scripting 2 07-16-2008 01:59 AM
time calculation liux99 UNIX for Advanced & Expert Users 2 08-18-2005 10:33 PM



All times are GMT -4. The time now is 03:55 AM.