Operating System - HP-UX
1847301 Members
2468 Online
110264 Solutions
New Discussion

Unix scripting Date formating

 
SOLVED
Go to solution
Edward_12
Occasional Contributor

Unix scripting Date formating

Hi there, I need some help.
I want to get the seconds after midnight. in the format 'sssss. I have tried:

H=$(date +%H)
M=$(date +%M)

S=$(($H*60*60)+($M*60))

It gives seconds for an old time.

Thanks
Jesus' King
7 REPLIES 7
Justo Exposito
Esteemed Contributor

Re: Unix scripting Date formating

Hi Edward,

Something like that?

integer sec=0
integer sec1=0
integer sec2=0
integer Hour=$(date +%R | cut -d":" -f1)
integer Minute=$(date +%R | cut -d":" -f2)
(( sec1 = $Hour * 60 * 60 ))
(( sec2 = $Minute * 60 ))
(( sec = $sec1 + $sec2 ))

echo $sec

Regards,

Justo.
Help is a Beatiful word
harry d brown jr
Honored Contributor

Re: Unix scripting Date formating

S=$(($H * 3600 + $M * 60 ))

live free or die
harry
Live Free or Die
Dave Chamberlin
Trusted Contributor

Re: Unix scripting Date formating

try this:
S = `exec $H \* 3600 + $M \* 60'
echo $S
James R. Ferguson
Acclaimed Contributor

Re: Unix scripting Date formating

Hi:

Seconds are available:

S=S=$(date +%S)

Then:

# let X=\($H*3600\)+\($M*60\)+$S

Regards!

...JRF...
Dave Chamberlin
Trusted Contributor

Re: Unix scripting Date formating

correction -
S = `exec $H \* 3600 + $M \* 60`
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Unix scripting Date formating

Here is one simple solution. One thing to avoid is multiple date calls.

S=$(date '+%T' | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3)')
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Unix scripting Date formating

Be sure you have balanced paraenthesis.

S=$((($H*60*60)+($M*60)))

The first $(( is for expressions calculation.
You need parenthesis to balance the formula.

-- Rod Hills
There be dragons...