Jump to content

military time convert or calc


Recommended Posts

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= ???

Link to comment
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)))

 

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

Link to comment
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 ) .

Link to comment
Share on other sites

we are done at post #4, right? 

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

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...