Hi
I have this date 11:30:02-31.11.12
How to calculate date between this date and now
?
I like it printed like this:
31D 2H 1M
if possible.
Hi
I have this date 11:30:02-31.11.12
How to calculate date between this date and now
?
I like it printed like this:
31D 2H 1M
if possible.
What have you tried so far?
Have you searched the forums for other date calculations?
What code do you have and where are the problems?
I know that I have to use date
function, and that I have to add some value, since now
is calculated since 1970.
Not sure how to make all this together.
Have a read of this:-
http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html
Robin
Liverpool/Blackburn
UK
I have write some answers about date calc, ex. function or ksh printf command.
Ex. using ksh93
#!/usr/bin/ksh93
str="11:30:02-31.11.12"
# convert - and . to spaces
str=${str/-/ }
str=${str//\./ }
# generic version
read time day month year << EOF
$str
EOF
# ksh93 can use also pipe reading
echo $str | read time day month year
year="20$year"
datestr="$year-$month-$day+$time"
epoc1=$(printf "%(%#)T" "$datestr")
epoc2=$(printf "%(%#)T" now )
((d=60*60*24))
((h=60*60))
(( diff=epoc2-epoc1 ))
(( daycnt=diff/d ))
(( hcnt=(diff-daycnt*d)/h ))
(( mcnt= (diff - daycnt*d - hcnt*h ) / 60 ))
printf "%dD %dH %dM\n" $daycnt $hcnt $mcnt
Or using gnu date command. Search from this site "date gnu calc"
What OS? If it's in Linux than it's very easy.
Its Ubuntu
I have figured out some, so think I will find the rest
diff=$(( ($(date "+%s")-$(date -d "2012/12/30 16:15:59" "+%s")) ))
days=$(($diff/60/60/24))
---------- Post updated at 10:33 ---------- Previous update was at 01:15 ----------
Solution found
Hope then can help other.
Happy new year to all...
test_log="2012/12/30 16:15:59"
diff=$(( ($(date "+%s")-$(date -d "${test_log}" "+%s")) ))
days=$(( $diff/86400 ))
hours=$(( ($diff-$days*86400)/3600 ))
minutes=$(( ($diff-$days*86400-$hours*3600)/60 ))
echo "${days}D ${hours}H ${minutes}M"
1D 18H 16M