date calc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date calc
# 1  
Old 09-12-2012
date calc

Hi,
I need subtract two date values (which are in day of the year format) and the output would give the remaining days. using the command date +"%j" i would get today's 'day of the year' i.e.,
Code:
> date +"%j"
256

Next, i need to take input of a previous date in the format 09/05/2012 and then convert it to 'day of the year format'. Could anyone please help how i could do this?
Note: I'm using HP-UX
# 2  
Old 09-12-2012
Do you have GNU date? if yes you can refer its man page.. if not you have to rely on good old days of date logic convert them to minutes and do minus then convert back to number of days or years so...
# 3  
Old 09-12-2012
# 4  
Old 09-14-2012
Code:
#/bin/ksh

# date format: mm/dd/yyyy

typeset -Z -R2 mm dd

y=${1##*/}
m=${1%%/*}
d=${1#*/}
d=${d%/*}
mm=$m
dd=$d
date="$mm/$dd/$y"

doy=0
f=0;
for mm in 1 2 3 4 5 6 7 8 9 10 11 12
do
   cal $mm $y | while read line
   do
      line=${line%%[a-zA-Z]*}
      [[ -n ${line:-""} ]] && {
         for dd in $line
         do
            (( doy = doy + 1 ))
            [[ "$mm/$dd/$y" = "$date" ]] && { f=1 ; break ; }
         done
         [[ "$mm/$dd/$y" = "$date" ]] && { break ; }
      }
   done
   [[ "$mm/$dd/$y" = "$date" ]] && { break ; }
done

[[ $f = 0 ]] && doy="Date not found"
echo $doy


Last edited by Corona688; 09-14-2012 at 02:05 PM..
# 5  
Old 09-14-2012
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Question for Perderabo on date calc

Hi, First of all, thanks for all the awesome suggestions on this forum. This helps all the UNIX enthusiast like me. Now, I had a similar requirement as mentioned in a very old post here: Question about Perderabo's "Days Elapsed Between Two Dates" But I am struggling what to change in the... (5 Replies)
Discussion started by: CleoBos
5 Replies

2. Shell Programming and Scripting

Calc Time Difference in Perl

Hello, I am having hard time calculating the time differnce in the below sequence. I tried nested for loops but I can't get to work. Algorithm: find time difference between the first AVAIL and the next event just before AVAIL. 0 05/17/2010 09:33 AVAIL <-- 1 1 05/17/2010 09:32 UM ... (2 Replies)
Discussion started by: bataf
2 Replies

3. Shell Programming and Scripting

Poker: calc profit in tournaments

Imagine that you play poker and the file 1.txt is your buy-in, and in 2.txt is your gains. I want to know the tournament that have more proffit and less proffit. 1.txt aa:1000 bb:2000 cc:3000 dd:4000 ee:5000 2.txt aa:0 bb:1000 cc:1500 dd:3000 ee:2000 Result: dd more profit; aa... (5 Replies)
Discussion started by: rafazz
5 Replies

4. Shell Programming and Scripting

Calc max of a column

In C that was easy with a for and if. Iam trying to learn a litle more in bash. Example Ronaldo:5800 Figo:4000 Rafael:2321 Kaka:1230 I want the max of the $2 and the output will be: The max value is 5800 from Ronaldo. How can i do this in shell? Thanks for all, folks. (11 Replies)
Discussion started by: rafazz
11 Replies

5. Programming

free disk space calc

I everybody!! How can i use statvfs() to calculate disk usage and free disk space?? Im using this code: /* Any file on the filesystem in question */ char *filename = "/home/nesto/test/test.cpp"; struct statvfs buf; if (!statvfs(filename, &buf)) { ... (1 Reply)
Discussion started by: ninjanesto
1 Replies

6. Shell Programming and Scripting

Calc number of days in a month

Looking for some help on capturing the number of days in a month to set as a loop counter. Any ideas, please let me know. (3 Replies)
Discussion started by: flounder
3 Replies
Login or Register to Ask a Question