Jump to content

I'm sure I'm missing an easy way to change a time format


fly
 Share

Recommended Posts

  • Moderators

I thought @Hour was 24 hour format?

$Hour = @Hour

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

_DateTimeFormat ()

in the beta

I guess I should have explained it better. I'm pulling the time from a remote machine. It is in AM/PM format. Once I have it in AM/PM format, I need to be able to switch it to 24 hour format.
Link to comment
Share on other sites

just read if it is AM or PM and add 12 to it.

you could probly do it in one line ...

Good point. But I was thinking that there was a conversion in there that I missed.

I'll do what you suggested tho. Thanks.

Link to comment
Share on other sites

Good point. But I was thinking that there was a conversion in there that I missed.

I'll do what you suggested tho. Thanks.

i'm not good enough with regular expressions to try a single line approach... here's what i've got

$time = "12:01 PM"
If StringInStr($time,"12") And StringInStr($time,"AM") Then $time = StringReplace($time,"12","00")
If StringInStr($time, "PM") And Number(StringLeft($time,StringInStr($time,":")-1)) < 12 Then
    $time = Number(StringLeft($time, StringInStr($time, ":") - 1)) + 12 & StringMid($time, StringInStr($time, ":"), 3)
Else
    $time = StringLeft($time, StringInStr($time, ":") + 2)
EndIf
MsgBox(0, "Time", $time)
Link to comment
Share on other sites

i was wrong

it took me 2 including some error checking

edit: used Tidy on the code

Func _24h($s_time)
    If StringRegExp($s_time, '(12)|(\d\d):\d\d PM') Then Return StringMid($s_time, 1, 2) + 12 & StringMid($s_time, 3, 3)
    If StringRegExp($s_time, '\d\d:\d\d .M') Then Return StringLeft($s_time, 5)
EndFunc  ;==>_24h

ConsoleWrite(_24h('01:15 AM') & @LF)
ConsoleWrite(_24h('01:15 PM') & @LF)

ConsoleWrite(_24h('12:15 AM') & @LF)
ConsoleWrite(_24h('12:15 PM') & @LF)
Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

i was wrong

it took me 2 including some error checking

edit: used Tidy on the code

Func _24h($s_time)
    If StringRegExp($s_time, '(12)|(\d\d):\d\d PM') Then Return StringMid($s_time, 1, 2) + 12 & StringMid($s_time, 3, 3)
    If StringRegExp($s_time, '\d\d:\d\d .M') Then Return StringLeft($s_time, 5)
EndFunc ;==>_24h

ConsoleWrite(_24h('01:15 AM') & @LF)
ConsoleWrite(_24h('01:15 PM') & @LF)

ConsoleWrite(_24h('12:15 AM') & @LF)
ConsoleWrite(_24h('12:15 PM') & @LF)
yeah i figured it could be done shorter with regexp, but that's one thing i really haven't gotten into... thanks for the example!
Link to comment
Share on other sites

Hmm... but what if it is first hour of the morning?

I mean, say it is 5 minutes after midnight. 12 hour clock would show 12:15 and 24 hour clock would show 00:15. But your function would show 24:05, wouldn't it?

Well, why guess... run it. It does show 24:15 for both 12:15 am and 12:15 pm, neither of which are correct.

- Steven

Edited by s_mack
Link to comment
Share on other sites

ugh why do you nubs use 12 hours anyway :P

is this better?

func _24h($s_time)

If StringMid($s_time, 7, 1) = 'P' AND Stringleft($s_time, 2) = '12' Then Return StringFormat('%.2d', StringLeft($s_time, 2)-12) & StringMid($s_time, 3, 3)

If StringMid($s_time, 7, 1) = 'P' Then return StringLeft($s_time, 2)+12 & StringMid($s_time, 3, 3)

return StringLeft($s_time, 5)

EndFunc

ConsoleWrite(_24h('01:15 AM') & @LF)

ConsoleWrite(_24h('01:15 PM') & @LF)

ConsoleWrite(_24h('12:15 AM') & @LF)

ConsoleWrite(_24h('12:15 PM') & @LF)

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

i'm not good enough with regular expressions to try a single line approach... here's what i've got

$time = "12:01 PM"
If StringInStr($time,"12") And StringInStr($time,"AM") Then $time = StringReplace($time,"12","00")
If StringInStr($time, "PM") And Number(StringLeft($time,StringInStr($time,":")-1)) < 12 Then
    $time = Number(StringLeft($time, StringInStr($time, ":") - 1)) + 12 & StringMid($time, StringInStr($time, ":"), 3)
Else
    $time = StringLeft($time, StringInStr($time, ":") + 2)
EndIf
MsgBox(0, "Time", $time)
The only problem with the code is that if $time = "12:12 AM", you get an undesired result (or xx:12 AM).

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

My Time Function

Func Time()
    Local $Light = " AM"
    Local $Hour = @HOUR
    Local $Minute = @MIN
    Local $Sec = @SEC
    If $Hour = 0 Then
        $Hour = 12
    ElseIf $Hour = 12 Then
        $Light = " PM"
    ElseIf $Hour > 12 Then
        $Hour = (@HOUR) - 12
        $Light = " PM"
    EndIf
    Local $Time = $Hour & ":" & $Minute & ":" & $Sec & $Light
    Return $Time
EndFunc  ;==>Time
Link to comment
Share on other sites

The only problem with the code is that if $time = "12:12 AM", you get an undesired result (or xx:12 AM).

ah... just a little re-write of the first condition check will fix that...

$time = "12:01 PM"
If StringInStr(StringLeft($time,2),"12") And StringInStr($time,"AM") Then $time = StringReplace($time,"12","00",1)
If StringInStr($time, "PM") And Number(StringLeft($time,StringInStr($time,":")-1)) < 12 Then
    $time = Number(StringLeft($time, StringInStr($time, ":") - 1)) + 12 & StringMid($time, StringInStr($time, ":"), 3)
Else
    $time = StringLeft($time, StringInStr($time, ":") + 2)
EndIf
MsgBox(0, "Time", $time)
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...