Sign in to follow this  
Followers 0
Necromorph

am/pm to 24 format

7 posts in this topic

im probably wasting time posting this, but i need a good way to take a already selected datetimepicker in the am/pm format and convert it to a 24 hour format. i have been browsing the help doc for a good func to do this. but thus far have been unsuccessful. thanks for your help in advanced.

Share this post


Link to post
Share on other sites



The value returned is a string; pretty easy to convert from am/pm to 24h format. am = 0-12, pm = am + 12

I'm pretty sure you can do it :(


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

$sTime = "3:24 aM"
MsgBox(0, "24 Hour", _Time_ConvertTo24($sTime))
$sTime = "3:24 PM"
MsgBox(0, "24 Hour", _Time_ConvertTo24($sTime))

Func Func _Time_ConvertTo24($s_Str)
    If NOT StringRegExp($s_Str, "^(?i)\d+.*:.*\d+\s*[ap]m$") Then Return $s_Str
    Local $aHold = StringRegExp($s_Str, "(\d+):(\d+)", 1), $sRtn, $sHour
    If @Error Then Return SetError(1)
    ;$aHold[0] = StringFormat("%02s", $aHold[0])
    If StringRegExp($s_Str, "(?i)^.+pm$") Then
        $sHour = $aHold[0]
        $sHour = $sHour +12
        If $sHour = 24 Then $sHour = 0
    Else
        $sHour = $aHold[0]
        If $sHour = 12 Then $sHour = 0
        $sRtn = $sHour & $aHold[1]
    EndIf
    Return StringFormat("%02u", $sHour) & $aHold[1]
EndFunc

Edit: minor code change to remove unnecessary variable

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

i got this one to work as long as your time is 'hh:mmtt'

Func _24Hour($time) 
        Local $PM = False
        If StringInStr($time, 'PM') Then $PM = True
        $time = StringLeft($time, 5)
        $hour = Number(StringLeft($time, 2))
        StringTrimRight($time, 2)
        $min = Number(StringRight($time, 2))
        If Not $PM And $hour = 12 Then Assign($hour, '0')
        If $PM And $hour < 12 Then $hour += 12
        Return $hour & ':' & $min
    EndFunc

doesn't handle 12:00am and 12:00pm _datediff() though, still working on that bug.

thanks for all of your help.

Share this post


Link to post
Share on other sites

i got this one to work as long as your time is 'hh:mmtt'

Func _24Hour($time) 
        Local $PM = False
        If StringInStr($time, 'PM') Then $PM = True
        $time = StringLeft($time, 5)
        $hour = Number(StringLeft($time, 2))
        StringTrimRight($time, 2)
        $min = Number(StringRight($time, 2))
        If Not $PM And $hour = 12 Then Assign($hour, '0')
        If $PM And $hour < 12 Then $hour += 12
        Return $hour & ':' & $min
    EndFunc

doesn't handle 12:00am and 12:00pm _datediff() though, still working on that bug.

thanks for all of your help.

First of all, dump that Assign() statement. It will work fine with

If Not $PM And $hour = 12 Then $hour = 0

Next, a properly formatted 24 hour time uses 2 characters for the hour. hence the StringFormat() call in the return of my function.

And lastly, your original post mentioned nothing about using _dateDiff() so perhaps you should explain exactly what you are attempting to do in order that we may give you more appropriate replies.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

sorry about all that, i did try your function, but if i use the _datediff it does not work when i use the 12:00pm time

ie:

starttime: 12:04pm

endtime: 02:04pm

it will return 0, and if i just use just the _datediff() it will return 8, it just subtracts the start and finish with no regaruds to the am/pm

i am getting frustrated in this functionality, i want to take a $varStartTime and a $varEndTime, keep them in an AM/PM format and find the differnce in them.

ie;

starttime: 09:15am

endtime: 3:30pm

totaltime: 6.25 hours

so any suggestions will be appreciated. thanks again.

actually the $hour = 0 solved my problems, so thanks!

Edited by redLabel

Share this post


Link to post
Share on other sites

Now that is the information which would have been worth knowing in your first post.

As long as it works now then all is well.

In the future please remember to give us those details which may be seemingly unimportant but we will have to know in order to correct your code issues.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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