Sign in to follow this  
Followers 0
fly

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

15 posts in this topic

How can I switch from AM/PM time to 24 hour time in a variable? I know I could write something to read the PM part, but is there a better way?

Share this post


Link to post
Share on other sites



I thought @Hour was 24 hour format?

$Hour = @Hour

[center]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.[/center]

Share this post


Link to post
Share on other sites

How can I switch from AM/PM time to 24 hour time in a variable? I know I could write something to read the PM part, but is there a better way?

_DateTimeFormat ()

in the beta


1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


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

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
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!

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
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())

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

A work of Art, w0uter!


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


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

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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