abdulrahmanok

military time convert or calc

18 posts in this topic

military time for example :

when normal time : 10 Am  military time : 1000

when normal time : 7:30 Am  military time : 0730

what i want is there is code to convert military to normal  time ? because i want to calculate the difference between time

for normal time it's easy 10 Am - 7.5 Am = 2.5 Hours But in military Time its very difficult 1000-0730= ???

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Maybe break it in two (10 and 00, do it with the other) and do calculations that way?

(this way you dont really need to convert it back to normal)

Edited by SorryButImaNewbie

Share this post


Link to post
Share on other sites

May be this help you :P 
 

Local $firstTime = '1000';
Local $secondTime = '0730';


MsgBox(0,"1000 to 10:00",miltToNormal($firstTime))
MsgBox(0,"0730 to 07:30",miltToNormal($secondTime))

Func miltToNormal($time)
    return StringLeft($time,2) & ":" & StringRight($time,2)
EndFunc

 

Share this post


Link to post
Share on other sites

If you just want math, then mil time is easier in my opinion

$time1 = "1145"
$time2 = "0730"

msgbox(0, '' , ((stringleft($time1 , 2) + stringright($time1 , 2) / 60)) - ((stringleft($time2 , 2) + stringright($time2 , 2) / 60)))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
5 minutes ago, Muhammad_Awais_Sharif said:

May be this help you :P 
 

Local $firstTime = '1000';
Local $secondTime = '0730';


MsgBox(0,"1000 to 10:00",miltToNormal($firstTime))
MsgBox(0,"0730 to 07:30",miltToNormal($secondTime))

Func miltToNormal($time)
    return StringLeft($time,2) & ":" & StringRight($time,2)
EndFunc

 

Ty Very much for super fast Response :)

Actually its converted but i failed to get Result between first time - second time

 

Local $firstTime = '1000';
Local $secondTime = '0730';
MsgBox(0,"1000 to 10:00",miltToNormal($firstTime))
MsgBox(0,"0730 to 07:30",miltToNormal($secondTime))

Func miltToNormal($time)
    return StringLeft($time,2) & ":" & StringRight($time,2)
EndFunc
MsgBox(0,"Re",$firstTime-$secondtime)

It gives me 270 whet it must be 230 ( 2 hours and 30 min ) .

Share this post


Link to post
Share on other sites

yeah you need to account for different possibilities (if 00 = 60 and others, I would think about it but dont have time :D )

 

good luck anyway

Share this post


Link to post
Share on other sites

we are done at post #4, right? 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

AutoIt it shipped with a UDF that handles time computations. first you must convert your military time to a calculable format, then diff the times and present in hours, like this:

#include <Date.au3>

Global $iTimeDiff = _DateDiff('n', _TimeConvert_MilitaryToCalc('0730'), _TimeConvert_MilitaryToCalc('1000')) / 60
MsgBox(0, '', $iTimeDiff)

Func _TimeConvert_MilitaryToCalc($sTime)
    Return @YEAR & '/' & @MON & '/' & @MDAY & ' ' & StringLeft($sTime, 2) & ':' & StringRight($sTime, 2) & ':' & '00'
EndFunc   ;==>_TimeConvert_MilitaryToCalc

look at the help for _DateDiff() and see why you need to convert the military time to calculable.

Edited by orbs
1 person likes this

Share this post


Link to post
Share on other sites

Check out _datediff in the help file...


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

15 minutes ago, iamtheky said:

If you just want math, then mil time is easier in my opinion

$time1 = "1145"
$time2 = "0730"

msgbox(0, '' , ((stringleft($time1 , 2) + stringright($time1 , 2) / 60)) - ((stringleft($time2 , 2) + stringright($time2 , 2) / 60)))

 

Ty very much It's worked like i want

sorry I didn't see your comment before :D Ty all

Solved .
Edit : You are Math genius :)

Edited by abdulrahmanok

Share this post


Link to post
Share on other sites

#11 ·  Posted

no, you should legitimately use the UDF as recommended later, that way your code is portable when future you needs to reuse the snippet.  Do it the right way, for future you!

But its always fun to roll your own to make sure you understand what is going on.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Have a look at Melba23's Date_Time_Convert UDF.  

Here is an example using the UDF.  

#include <Date.au3>
#include <DTC.au3>

Global $sTime1 = "7:30 AM"
Global $sTime2 = "10:00 AM"

;Convert to Mil time.
Global $sMilTime1 = _Date_Time_Convert($sTime1, "h:mm TT", "HHmm")
Global $sMilTime2 = _Date_Time_Convert($sTime2, "h:mm TT", "HHmm")
ConsoleWrite("Mil Time" & @CRLF & $sMilTime1 & @CRLF & $sMilTime2 & @CRLF & @CRLF)

;Convert to 12 hr time.
Global $s12HrTime1 = _Date_Time_Convert($sMilTime1, "HHmm", "h:mm TT")
Global $s12HrTime2 = _Date_Time_Convert($sMilTime2, "HHmm", "h:mm TT")
ConsoleWrite("12 Hr Time" & @CRLF & $s12HrTime1 & @CRLF & $s12HrTime2 & @CRLF & @CRLF)

;Convert to calculable time.
Global $sCalcTime1 = _Date_Time_Convert($sTime1, "h:mm TT", "HH:mm")
Global $sCalcTime2 = _Date_Time_Convert($sTime2, "h:mm TT", "HH:mm")
ConsoleWrite("Calc Format Time" & @CRLF & $sCalcTime1 & @CRLF & $sCalcTime2 & @CRLF & @CRLF)

;Get today's date.
Global $sToday = _NowCalcDate()

;Calculate the difference between the two times.
Global $sTimeDiff = _DateDiff("n", $sToday & " " & $sCalcTime1, $sToday & " " & $sCalcTime2) / 60
ConsoleWrite("Time difference between " & $sTime1 & " and " & $sTime2 &  ": " & $sTimeDiff & " Hr" & ($sTimeDiff > 1 ? "s" : "") & @CRLF & @CRLF)

 

Adam

 

Edited by AdamUL

Share this post


Link to post
Share on other sites

#13 ·  Posted

Just a sidenote: "normal time" like 00:00 AM is it "normal time" like 00:00 PM? Same issue -but more ambiguously- arises with 12:00 AM vs. PM where what that means depends on whom you're talking to and the convention each end is using.

"Normal" in this thread just means "like ambiguous US time" or am I misunderstanding something?

I'd say "normal time" is 24h explicit time.
$0.02


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

they are both normal, they are the only times that occur simultaneously.  Ambiguity would infer that they were different things (or that if misunderstood could lead to a wrong conclusion) rather than just different names for the same thing. (my attempt at the most ambiguous answer ever may have failed and the humor lost, but at least i tried).

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#15 ·  Posted

Consider this, the op has stated that he wants to calculate the difference in time (duration). 

Using this code yields an ambiguous if not erroneous result...

$time1 = "0800"
$time2 = "0700"

msgbox(0, '' , ((stringleft($time1 , 2) + stringright($time1 , 2) / 60)) - ((stringleft($time2 , 2) + stringright($time2 , 2) / 60)))

Because $time2 is smaller than $time1 it could be inferred that a date boundary has been crossed, hence the ambiguity/error.

Conclusion:

Better to use the supplied UDF with complete date/times supplied by the user.

My $.02


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#16 ·  Posted

Are you betting your life on that?

Look at the table mid-page of https://en.wikipedia.org/wiki/12-hour_clock under the heading "Confusion at noon and midnight".

I'd call that table fairly confusing about the vertues of 12-hour notationS.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#17 ·  Posted

@iamtheky

@AdamUL

@kylomas

@jchd

Thank You All for Your Suggestions I will working on it but later because I have to Complete Some project now :drool:

 

 

 

Share this post


Link to post
Share on other sites

#18 ·  Posted

10 hours ago, iamtheky said:

we are done at post #4, right? 

Yes ,Thank You very much .

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now