argumentum Posted Thursday at 12:48 AM Posted Thursday at 12:48 AM (edited) ; #FUNCTION# ==================================================================================================================== ; Name...........: HourAmPm ; Description....: Converts a time in 24-hour format to AM/PM format. ; Syntax.........: HourAmPm( $sDateTime [, $sAmPm = "AM|PM" [, $iTrimRight = 0]] ) ; Parameters.....: $sDateTime - The time (with date or not) string with "HH:" in it. ; $sAmPm - [optional] The AM/PM representation (default is "AM|PM"). ; $iTrimRight - [optional] The number of characters to trim from the right of the result (default is 0). ; $iNoDate - [optional] Whether to omit the date from the output. Defaults to False. ; Return values .: Success: Returns the formatted date and time in AM/PM format. ; Failure: None. ; Author ........: argumentum ; Modified ......: ; Remarks .......: This function takes a 24-hour time string, converts it to AM or PM format, and returns the result with optional trimming. ; Related .......: ; Link ..........: https://www.autoitscript.com/forum/index.php?showtopic=213061 ; Example .......: MsgBox(64, "Converted Time", HourAmPm("12/31/1999 18:59:59")) ; =============================================================================================================================== Func HourAmPm($sDateTime, $sAmPm = Default, $iTrimRight = Default, $iNoDate = Default) Local $aAmPm = StringSplit((StringInStr($sAmPm, "|") ? $sAmPm : "AM|PM"), "|"), $sFormat = $aAmPm[2] Local $iHourPos = StringInStr($sDateTime, ":"), $sHour = StringMid($sDateTime, $iHourPos - 2, 2) Local $sDate = StringLeft($sDateTime, $iHourPos - 3), $sTime = StringTrimLeft($sDateTime, $iHourPos - 1) If $sHour < 12 Then $sFormat = $aAmPm[1] ; https://www.autoitscript.com/forum/index.php?showtopic=213061 $sHour = Mod($sHour, 12) If Not $sHour Then $sHour = 12 Return StringTrimRight((Int($iNoDate) ? "" : $sDate) & StringRight('0' & $sHour, 2) & $sTime, Int($iTrimRight)) & " " & $sFormat EndFunc ;==>HourAmPm ...am always looking for these ( because am disorganized ) and when I find them they are more than needed so, I decided to simplify it and flexibly-cate** it. All that's needed is the hour, the rest of the date-time string should be anything as long as there is a "HH:" in it. ; Examples: ConsoleWrite('- ' & HourAmPm("18:59") & @CRLF) ; - 06:59 PM ConsoleWrite('- ' & HourAmPm("18:59:59.999") & @CRLF) ; - 06:59:59.999 PM ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999") & @CRLF) ; - 1999/12/31 06:59:59.999 PM ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999", Default, 7) & @CRLF) ; - 1999/12/31 06:59 PM ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59", Default, 3) & @CRLF) ; - 1999/12/31 06:59 PM ConsoleWrite('- ' & HourAmPm("12/31/1999 18:59", "a|p") & @CRLF) ; - 12/31/1999 06:59 p ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999", Default, 7, True) & @CRLF) ; - 06:59 PM Don't remember seeing this approach anywhere so I decided to share it **flexibly-cate [verb] To make it flexible Edited Thursday at 03:49 PM by argumentum better ioa747, Numeric1 and Parsix 3 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
jchd Posted Thursday at 08:20 AM Posted Thursday at 08:20 AM Thanks. Nitpick: if hours is a single digit, a preceding space is eaten. I seem to recall that H, M & S format is 2 digit in all cases. "2025-08-07 0:0:0" returns "2025-08-0712:0:0 AM" but should probably be "2025-08-07 12:00:00 AM" Not that I use AM/PM ridiculous convention. 24h format, like metric units, should be mandatory everywhere. ioa747 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
ioa747 Posted Thursday at 09:45 AM Posted Thursday at 09:45 AM (edited) Practical! Thank you for sharing it. ideas - thoughts Spoiler expandcollapse popup#include <Date.au3> ; Examples: ConsoleWrite('- ' & HourAmPm("18:59") & @CRLF) ConsoleWrite('- ' & HourAmPm("18:59:59.999") & @CRLF) ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999") & @CRLF) ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999") & @CRLF) ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59") & @CRLF) ConsoleWrite('- ' & HourAmPm("12/31/1999 18:59") & @CRLF) ConsoleWrite('- ' & HourAmPm("2025-08-07 0:0:0") & @CRLF) ConsoleWrite('- ' & HourAmPm("2025-08-07 0:0:0") & @CRLF) ConsoleWrite('- ' & HourAmPm() & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 0, "ante meridiem/post meridiem") & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 0, "midnight/midday") & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 0, "vorm./nachm.") & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 0, "π.μ./μ.μ.") & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 1, "오전/오후") & @CRLF) ConsoleWrite('- ' & HourAmPm(Default, 0, "a.m./p.m.") & @CRLF) Func HourAmPm($sDateTime = "", $iSignUpper = 1, $sSign = "AM/PM") Local $aSign = StringSplit($sSign, "/", 2) Local $sAM = ($iSignUpper ? StringUpper($aSign[0]) : StringLower($aSign[0])) Local $sPM = ($iSignUpper ? StringUpper($aSign[1]) : StringLower($aSign[1])) Local $sFormat = $sAM, $iHour, $sTimePart, $sDatePart ; If no time string is provided, use the current local time. ; Local $sNow = @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC If $sDateTime = "" Or $sDateTime = Default Then $sDateTime = _Now() ; _Now() to local format ; Separate date and time parts, if a space exists. Local $iSpacePos = StringInStr($sDateTime, " ", 0, -1) If $iSpacePos > 0 Then $sDatePart = StringLeft($sDateTime, $iSpacePos) & " " $sTimePart = StringRight($sDateTime, StringLen($sDateTime) - $iSpacePos) Else $sDatePart = "" $sTimePart = $sDateTime EndIf ; Find the position of the first colon in the time part. Local $iColonPos = StringInStr($sTimePart, ":") If $iColonPos = 0 Then Return $sDateTime & " (Error: Invalid time format)" ; Extract the hour from the time part. $iHour = Number(StringLeft($sTimePart, $iColonPos - 1)) ; Determine if it's AM or PM. If $iHour >= 12 Then $sFormat = $sPM ; Convert to 12-hour format. If $iHour > 12 Then $iHour -= 12 ElseIf $iHour = 0 Then $iHour = 12 EndIf ; Get the rest of the time string (minutes and seconds) correctly. $sTimePart = StringTrimLeft($sTimePart, $iColonPos - 1) ; Return the final string. Return $sDatePart & StringRight("0" & $iHour, 2) & $sTimePart & " " & $sFormat EndFunc ;==>HourAmPm Edited Thursday at 11:02 AM by ioa747 improvement I know that I know nothing
argumentum Posted Thursday at 11:57 AM Author Posted Thursday at 11:57 AM (edited) 3 hours ago, jchd said: Not that I use AM/PM ridiculous convention. 24h format, like metric units, should be mandatory everywhere. And !, I don't disagree. But I've been here for so long that my brain has a hard time..., feeling, what time it is. As far as time formats, the one that comes out of AutoIt's Date.au3 UDF is YYYY/MM/DD HH:MM:SS and this HourAmPm() is just for that scenario. I have become personally fond of the YYYY/MM/DD format and that !, should be mandatory everywhere too 2 hours ago, ioa747 said: If $iColonPos = 0 Then Return $sDateTime & " (Error: Invalid time format)" I added the "( yes, one more )" to the title, because there are a bunch of all encompassing AM/PM functions in the forum that I found to be "overengineered". I use this in my personal stuff: and is not American or any counties standard. Is AutoIt's standard that I liked, plus the AM/PM I did think of that ( $sSign = "AM/PM" ) , but I'll replace $iSignUpper with $sSign = "AM/PM", as it does make it more usable. That I'll change. Edited Thursday at 11:58 AM by argumentum English ioa747 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
argumentum Posted Thursday at 12:13 PM Author Posted Thursday at 12:13 PM The function header is thanks to your "press F10" toy @ioa747 ioa747 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Solution argumentum Posted Thursday at 03:52 PM Author Solution Posted Thursday at 03:52 PM (edited) ConsoleWrite('- ' & HourAmPm("1999/12/31 18:59:59.999", Default, 7, True) & @CRLF) ; - 06:59 PM Added "NoDate" because in the pic shows a full date and time but also say in 90 minutes so, might as well exclude the date because, dah Edited Thursday at 04:00 PM by argumentum ioa747 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
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