Sign in to follow this  
Followers 0
Sven

AM / PM (2x 12hour) to 24hour conversion; is this correct?

4 posts in this topic

After a marathon of coding I've reached the point where I can't think straight anymore.

All i'm trying to do is to convert any hour from the AM / PM system to the 24hour system, and am wondering if I got the function below right or not.

$test = _convert_hour(12, "am")
msgbox(0, "test", $test)

Func _convert_hour($hour, $am_pm)
    $am_pm = StringLower($am_pm)
    If $am_pm = "am" And $hour = 12 Then return ($hour - 12)
    If $am_pm = "pm" And $hour >= 1 And $hour <= 11 Then return ($hour + 12)
    return ($hour)
EndFunc   ;==>_convert_hour

Thank you for your feedback.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MsgBox(0,"", _TimeHrMin()) ; 24 to am/pm format
MsgBox(0,"", _TimeHrMin2()) ; am/pm format to 24
MsgBox(0,'', @HOUR & ":"& @MIN & ":"& @SEC & "...Format 24") ; 24 to 24 format

Func _TimeHrMin()
    $hr = @HOUR
    If $hr > 12 Then
        $hr = $hr -12
        $x = "PM"
    Else
        $x = "AM"
    EndIf
    $time = $hr & ":" & @MIN & " " & $x
    Return
Func _TimeHrMin2()
    $hr = @HOUR
    If $hr < 12 Then
        $x = "PM/AM"
    EndIf
    $time = $hr & ":" & @MIN & " " & $x
    Return ($time)
EndFunc ($time)
EndFunc

Edited by metis

Share this post


Link to post
Share on other sites

Here another method:

Func Convert24h($time, $format) ;am/pm to 24h format by UEZ
    If $format = "am" Then Return Mod($time, 12)
    Return Mod($time + 12, 24)
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks, greatly appreciated. UEZs method is very elegant.

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
Sign in to follow this  
Followers 0