fly Posted December 9, 2005 Share Posted December 9, 2005 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? Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 9, 2005 Moderators Share Posted December 9, 2005 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 More sharing options...
seandisanti Posted December 9, 2005 Share Posted December 9, 2005 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 Link to comment Share on other sites More sharing options...
fly Posted December 9, 2005 Author Share Posted December 9, 2005 _DateTimeFormat ()in the betaI 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 More sharing options...
w0uter Posted December 9, 2005 Share Posted December 9, 2005 just read if it is AM or PM and add 12 to it. you could probly do it in one line ... 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 More sharing options...
fly Posted December 9, 2005 Author Share Posted December 9, 2005 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 More sharing options...
seandisanti Posted December 9, 2005 Share Posted December 9, 2005 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 More sharing options...
w0uter Posted December 9, 2005 Share Posted December 9, 2005 (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 December 9, 2005 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 More sharing options...
seandisanti Posted December 9, 2005 Share Posted December 9, 2005 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 More sharing options...
Guest s_mack Posted December 9, 2005 Share Posted December 9, 2005 (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 December 9, 2005 by s_mack Link to comment Share on other sites More sharing options...
w0uter Posted December 9, 2005 Share Posted December 9, 2005 (edited) ugh why do you nubs use 12 hours anyway 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 December 9, 2005 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 More sharing options...
MSLx Fanboy Posted December 10, 2005 Share Posted December 10, 2005 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 More sharing options...
themax90 Posted December 10, 2005 Share Posted December 10, 2005 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 More sharing options...
jefhal Posted December 10, 2005 Share Posted December 10, 2005 A work of Art, w0uter! ...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format Link to comment Share on other sites More sharing options...
seandisanti Posted December 10, 2005 Share Posted December 10, 2005 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now