How to convert 24 Hr Time Format into Minutes?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert 24 Hr Time Format into Minutes?
# 1  
Old 07-02-2013
How to convert 24 Hr Time Format into Minutes?

Hello All

I know the general Logic behind it but do not know the shell programming so much.


For Example, The Time is stored in a given Variable

if the Time is 0800 then i need to extract the last digits of the number and Add it to the Remaining Digit of the Number which is multiplied by 60.

08 00 will be 08 * 60 + 00 = 480 Minutes

Or 1530 will be 15*60 + 30 = 930 minutes.



Regards
Ajesh
# 2  
Old 07-02-2013
something like this?

Code:
 
echo "1530"|awk '{print substr($0,1,2)*60+substr($0,3)}'

# 3  
Old 07-02-2013
Code:
[user@host ~]$ x=1530
[user@host ~]$ y=$(( $(( $(( $(( $x - $(( $x % 100 )) )) / 100 )) * 60 )) + $(( $x % 100 )) ))
[user@host ~]$ echo $y
930

# 4  
Old 07-02-2013
Quote:
Originally Posted by balajesuri
Code:
[user@host ~]$ x=1530
[user@host ~]$ y=$(( $(( $(( $(( $x - $(( $x % 100 )) )) / 100 )) * 60 )) + $(( $x % 100 )) ))
[user@host ~]$ echo $y
930

Code:
$ x=1530
$ y=$(( (((x - ( x % 100 )) / 100 ) * 60 ) + ( x % 100 ) ))
$ echo $y
930

# 5  
Old 07-02-2013
Assuming bash shell:
Code:
$ x=1530
$ y=$(( (x / 100)*60 + (x % 100) ))
$ echo $y
930

This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 07-02-2013
Quote:
Originally Posted by fpmurphy
Assuming bash shell:
Code:
$ x=1530
$ y=$(( (x / 100)*60 + (x % 100) ))
$ echo $y
930

That works with any POSIX conforming shell (such as a 1993 or later version of the Korn shell); not just bash. Another way to do it with any POSIX conforming shell is:
Code:
for x in 0800 0815 0959 1530
do	y=$(( ${x%??} * 60 + ${x#??} ))
	echo $x $y
done

which produces the following output:
Code:
0800 480
0815 495
0959 599
1530 930


Last edited by Don Cragun; 07-03-2013 at 02:18 PM.. Reason: Fix typo
# 7  
Old 07-02-2013
There is always the interactive mode to learn from:-

Code:
Last login: Tue Jul  2 17:58:45 on ttys000
AMIGA:barrywalker~> hhmm=0457
AMIGA:barrywalker~> hh=${hhmm:0:2}
AMIGA:barrywalker~> echo $hhmm
0457
AMIGA:barrywalker~> echo $hh
04
AMIGA:barrywalker~> hh=$[ ( hh * 60 ) ]
AMIGA:barrywalker~> echo $hh
240
AMIGA:barrywalker~> mm=${hhmm:2:2}
AMIGA:barrywalker~> echo $mm
57
AMIGA:barrywalker~> mins=$[ ( hh + mm ) ]
AMIGA:barrywalker~> echo $mins
297
AMIGA:barrywalker~>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

3. Shell Programming and Scripting

Convert epoch time stamp into human readable format

Can someone help me to write a shell script to convert epoch timestamp into human readable format 1394553600,"test","79799776.0","19073982.728571","77547576.0","18835699.285714" 1394553600,"test1","80156064.0","19191275.014286","62475360.000000","14200554.720000"... (10 Replies)
Discussion started by: Moon1234
10 Replies

4. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

5. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

6. Shell Programming and Scripting

Perl - Extract 12 hour time, convert to 24 and subtract 15 minutes?

OK, I am by no means a programmer... I have been given the task to do some automation scripts. I have got most of it working from snippets I have found on the Web. One requirement has me stumped. The initial timing file created by the user is a comma delimited in the following format.... (4 Replies)
Discussion started by: autotuner
4 Replies

7. UNIX for Advanced & Expert Users

Convert to time format

I Have variable $currenttime and I want to display this variable as a TIME FORMAT and the $currenttime value contains like 000000 120000 020000 I want to display to 00:00:00 12:00:00 02:00:00 please help me how to this one (1 Reply)
Discussion started by: gksenthilkumar
1 Replies

8. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

9. Shell Programming and Scripting

how to convert epoch time to readible format?

Hi, I would like to convert epoch time from the logs to readible fromat. How do I do it within shell? Thanks! (11 Replies)
Discussion started by: cin2000
11 Replies

10. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies
Login or Register to Ask a Question