Date time calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date time calculation
# 1  
Old 07-29-2010
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!
# 2  
Old 07-29-2010
How about this?
# 3  
Old 07-29-2010
Hi,

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

# 4  
Old 07-29-2010
Quote:
Originally Posted by pravin27
Hi,

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

This is GNUism - may not be applicable.
# 5  
Old 07-29-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Time calculation

Hi Gurus, I need to get one hour before time is yyyymmddhh format. ex. date +"%Y%m%d%H" gives 2017052814 but I need 2017052813 Thankx Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: nalakaatslt
6 Replies

2. UNIX for Dummies Questions & Answers

Need help regarding date calculation

Hi All, I need to calculate the date for monday to friday. Say today is 10.oct.2014 then I need to calculate the date as 09.oct.2014. If today is 13.oct.2014 then I need to calculate the date as 10.oct.2014. If its 03.Nov.2014 then date calculated must be 31.oct.2014. Please help in in... (2 Replies)
Discussion started by: abhi_123
2 Replies

3. Shell Programming and Scripting

How to do simple date (time) calculation in shell script?

Hi, I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes. For example... STARTTIME=whenever this script starts ./path/to/command.sh >>logfile.log TOTALTIME=<time at this stage of the script after above command... (7 Replies)
Discussion started by: nbsparks
7 Replies

4. Shell Programming and Scripting

Date calculation, a little different...

Hello eveyone, I am tasked to write an (n)awk script to calculate the days between dates. Now before anyone chastizes me on how many forums there are on date calculation, I have no seen one that takes into account the Julian calender as well. As most know, September of 1752 is when we (the US)... (2 Replies)
Discussion started by: csharp100
2 Replies

5. Shell Programming and Scripting

Date calculation

I have to run a shell , only if file_a.txt is updated correctly before 1hr. that is, file_a.txt 2011_09_21__14:10:20 --> any format.. And the shell can be like, if ; then Run some shell update file_a.txt with (date now) fi how to do this ? (6 Replies)
Discussion started by: linuxadmin
6 Replies

6. Shell Programming and Scripting

Time stamp calculation

Hi all; I'm relatively new to scripting,I am working on a monitoring script.....where in i have to write subroutine which does the follows: It will check the time stamp of a file ( Oracle remarchive files) and compare it with existing time.If the time difference happen to be more than 90... (6 Replies)
Discussion started by: maverick_here
6 Replies

7. UNIX for Dummies Questions & Answers

Time Calculation

I have a file with over 100,000 lines of data with looking to compare times of about 2000 lines to get a total time of a process. The lines of unique data are as follows. FINER: CacSoapServer:reserveNetworkResource got the sessionID and INFO: Created CAC session ID The command... (5 Replies)
Discussion started by: bpfoster76
5 Replies

8. Shell Programming and Scripting

Date Calculation

Hi experts, Can you please help me in calculating the future date using unix shell scripting.. for example lets say todays date is 29-sep-2008 i wanna calculate the date after 365 days... hence the date must be 29-sep-2009.. Quick response is appreciable. thanks in... (2 Replies)
Discussion started by: subhendu81
2 Replies

9. Shell Programming and Scripting

Time difference calculation

Hi Team, I am currently in the process of writing a script which will take a filename in the format SKADEV.0.db2.NODE0000.CATN0000.20080714231015.001 where the sixth string(with "." as the seperator) is the time stamp of the time of creation of the file. now here is my issue . I need to be... (2 Replies)
Discussion started by: Segwar
2 Replies

10. UNIX for Advanced & Expert Users

time calculation

Hi, I have start time as a string like 06:04:01 and end time like 06:05:01 i need do a simple math to get the duration. What is the best way to do this in Korn Shell scripting? Thanks (2 Replies)
Discussion started by: liux99
2 Replies
Login or Register to Ask a Question