Jump to content

Hour AM/PM ( yes, one more of these )


Go to solution Solved by argumentum,

Recommended Posts

Posted (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 :P

Edited by argumentum
better

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

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.

 

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 here
RegExp tutorial: enough to get started
PCRE 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)

Posted (edited)

Practical! Thank you for sharing it.

ideas - thoughts  :think:

Spoiler
#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 by ioa747
improvement

I know that I know nothing

Posted (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:
image.png.9580f621d454c5c5dfb048ca19b95203.png     image.png.819642c3f80a815e2f1d729138106537.png

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 by argumentum
English

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

  • 1 month later...
Posted

Is my understanding correct that the latest/greatest AutoIt version does not allow to translate date/time from am/pm format to 24 hours format?
I searched the forum but couldn't find an answer.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted
32 minutes ago, water said:

...AutoIt version does not allow to translate date/time...

I care to change it to AM/PM because everything is 24 hour based.
Do you have a AM/PM scenario that needs to be switched to 24 hour format ?
And what do you mean by "does not allow to translate date/time" ? _DateTimeFormat ( $sDate, $sType )  in the new release is expanded for AM/PM

#include <Date.au3>
; Show current date/time in the pc's format
ConsoleWrite('+ _NowCalc() >' & _NowCalc() & '<' & @CRLF)
For $n = 0 To 7
    ConsoleWrite('- _DateTimeFormat(_NowCalc(), ' & $n & ') >' & _DateTimeFormat(_NowCalc(), $n) & '<' & @CRLF)
Next
+ _NowCalc() >2025/09/24 13:41:22<
- _DateTimeFormat(_NowCalc(), 0) >9/24/2025 1:41:22 PM<
- _DateTimeFormat(_NowCalc(), 1) >Wednesday, September 24, 2025<
- _DateTimeFormat(_NowCalc(), 2) >9/24/2025<
- _DateTimeFormat(_NowCalc(), 3) >1:41:22 PM<
- _DateTimeFormat(_NowCalc(), 4) >13:41<
- _DateTimeFormat(_NowCalc(), 5) >13:41:22<
- _DateTimeFormat(_NowCalc(), 6) ><
- _DateTimeFormat(_NowCalc(), 7) ><

hmm !, I see what you mean !. Last I saw it worked but the release does not work. I'll look into it. 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

On the german forum there is a guy who needs to translate date/time from AM / PM format to 24 hours format.
His problem has already been solved, but I remember your thread in the engl. forum regarding AM / PM output formatting.
I searched the forum for code to do the AM /PM > 24 hours translation - without success.
So I decided to ask the master of date / time translation ;) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted (edited)
41 minutes ago, water said:

I searched the forum for code to do the AM /PM > 24 hours translation - without success.

Ok, am getting better at screwing up 😅
That function ( _DateTimeFormat() ) does not do that. I'd have to look into it.

41 minutes ago, water said:

So I decided to ask the master of date / time translation ;) 

Like my AI tells me: "you think too much of me" :lol:

I'd have to get my brain in gear for that and am already spread too thin as is right now.
@SOLVE-SMART gave him a solution for that in the German forum that works well ? ( haven't tested it ), unless that is not what he needs or you'd like to include that functionality in this function too, to have an all encompassing function to handle everything.

Nonetheless your post woke me up to the :oops:in the date UDF.

Edit: 

42 minutes ago, water said:

His problem has already been solved, but I remember your thread in the engl. forum regarding AM / PM output formatting.

...I need some cafe...

 

Edited by argumentum
oops

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
4 minutes ago, argumentum said:

Yes, it works perfectly. He replied to Solve-Smart's post: "That's exactly what I was looking for".
So I would wait for more users to ask for the AM /PM > 24 hours translation before thinking about adding this functionality to AutoIt. 

Thanks a lot for taking the time to look into this subject :) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

@water... I'll get me some cafe, take a walk, ..something. I edited my post above. I took everything out of context. My brain is hooked on a work project and I can not do more than one thing at the time :baby:

3 minutes ago, water said:

So I would wait for more users to ask for the AM /PM > 24 hours translation before thinking about adding this functionality to AutoIt.

Ok. That addition to the Date.au3 UDF, was right there for the taking. All I did was to add the strings for options 6 and 7

...
        Case 6
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm tt"
            EndIf
        Case 7
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm:ss tt"
            EndIf
...

Now including the AM/PM to it, ...I'll need to be in a happy place ( no pressure ) to get my brain in gear for that.

Now any and every one, is welcomed to present a _DateTimeFormat() that includes that. I'll ( well, the MVPs ) will look at it and say "yey/ney".
Remember ( not just you ), that all these UDFs were scripts users of the forum coded and expanded with the participation of everyone.
Don't be shy. Your nationforum needs you !. ( nowalways recruiting coders ) :) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

Wanted something more,  :)

added

...
    ; If $sType < 0 Or $sType > 5 Or Not IsInt($sType) Then
    If $sType < 0 Or $sType > 7 Or Not IsInt($sType) Then
...
        Case 6
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm tt"
            EndIf
        Case 7
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm:ss tt"
            EndIf
...
        If (StringInStr($sDate, 'pm') > 0) Or (StringInStr($sDate, $sPM) > 0) Then
            If $asTimePart[1] < 12 Then $asTimePart[1] += 12
        ElseIf (StringInStr($sDate, 'am') > 0) Or (StringInStr($sDate, $sAM) > 0) Then
            If $asTimePart[1] = 12 Then $asTimePart[1] = 0
        EndIf
...

$sDate Input date in the format "YYYY/MM/DD[ HH:MM:SS[ tt]]" 
...
4 - Display a time using the 24-hour format (hh:mm).      (conversion from am/pm format).
5 - Display a time using the 24-hour format (hh:mm:ss).  (conversion from am/pm format).
6 - Display a time using the am/pm format (hh:mm tt). 
7 - Display a time using the am/pm format (hh:mm:ss tt).

completed

#include <Date.au3>

;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:70:00', 5) & @CRLF)       ; -
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:30:00', 5) & @CRLF)       ; - 08:30:00
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 20:30:00', 5) & @CRLF)       ; - 20:30:00
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 00:00:00', 5) & @CRLF)       ; - 00:00:00
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 12:00:44', 5) & @CRLF)       ; - 12:00:44
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 07:05:00', 4) & @CRLF)       ; - 07:05
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 07:05:00', 4) & @CRLF)       ; - 07:05

ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:70:00 AM', 5) & @CRLF)    ; -
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:30:00 AM', 5) & @CRLF)    ; - 08:30:00
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:30:00 PM', 5) & @CRLF)    ; - 20:30:00
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 12:00:00 AM', 5) & @CRLF)    ; - 00:00:00
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 12:00:44 PM', 5) & @CRLF)    ; - 12:00:44
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 07:05:00 pm', 4) & @CRLF)    ; - 19:05
ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 07:05:00 am', 4) & @CRLF)    ; - 07:05

;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:70:00', 7) & @CRLF)       ; -
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 08:30:00', 7) & @CRLF)       ; - 08:30:00 AM
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 20:30:00', 7) & @CRLF)       ; - 08:30:00 PM
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 00:00:00', 7) & @CRLF)       ; - 12:00:00 AM
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 12:00:44', 7) & @CRLF)       ; - 12:00:44 PM
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 19:05:00', 6) & @CRLF)       ; - 07:05 PM
;~ ConsoleWrite("- " & _DateTimeFormatEx('2025/09/01 07:05:00', 6) & @CRLF)       ; - 07:05 AM


;~ $sDate Input date in the format "YYYY/MM/DD[ HH:MM:SS[ tt]]" 
;~ $sType one the following:
;~ 0 - Display a date and/or time. If there is a date part, display it as a short date.
;~ If there is a time part, display it as a long time. If present, both parts are displayed.
;~ 1 - Display a date using the long date format specified in your computer's regional settings.
;~ 2 - Display a date using the short date format specified in your computer's regional settings.
;~ 3 - Display a time using the time format specified in your computer's regional settings.
;~ 4 - Display a time using the 24-hour format (hh:mm).    (conversion from am/pm format).
;~ 5 - Display a time using the 24-hour format (hh:mm:ss). (conversion from am/pm format).
;~ 6 - Display a time using the am/pm format (hh:mm tt).
;~ 7 - Display a time using the am/pm format (hh:mm:ss tt).

Func _DateTimeFormatEx($sDate, $sType)
    Local $asDatePart[4], $asTimePart[4]
    Local $sTempDate = "", $sTempTime = ""
    Local $sAM, $sPM, $sTempString = ""
    ; Verify If InputDate is valid
    If Not _DateIsValid($sDate) Then
        Return SetError(1, 0, "")
    EndIf
    ; input validation
    If $sType < 0 Or $sType > 7 Or Not IsInt($sType) Then
        Return SetError(2, 0, "")
    EndIf
    ; split the date and time into arrays
    _DateTimeSplit($sDate, $asDatePart, $asTimePart)

    Switch $sType
        Case 0
            $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_SSHORTDATE) ; Get short date format.
            If Not @error And Not ($sTempString = '') Then
                $sTempDate = $sTempString
            Else
                $sTempDate = "M/d/yyyy"
            EndIf
            If $asTimePart[0] > 1 Then
                $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_STIMEFORMAT) ; Get short time format.
                If Not @error And Not ($sTempString = '') Then
                    $sTempTime = $sTempString
                Else
                    $sTempTime = "h:mm:ss tt"
                EndIf
            EndIf
        Case 1
            $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_SLONGDATE) ; Get long date format.
            If Not @error And Not ($sTempString = '') Then
                $sTempDate = $sTempString
            Else
                $sTempDate = "dddd, MMMM dd, yyyy"
            EndIf
        Case 2
            $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_SSHORTDATE) ; Get short date format.
            If Not @error And Not ($sTempString = '') Then
                $sTempDate = $sTempString
            Else
                $sTempDate = "M/d/yyyy"
            EndIf
        Case 3
            If $asTimePart[0] > 1 Then
                $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_STIMEFORMAT) ; Get short time format.
                If Not @error And Not ($sTempString = '') Then
                    $sTempTime = $sTempString
                Else
                    $sTempTime = "h:mm:ss tt"
                EndIf
            EndIf
        Case 4
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm"
            EndIf
        Case 5
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm:ss"
            EndIf
        Case 6
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm tt"
            EndIf
        Case 7
            If $asTimePart[0] > 1 Then
                $sTempTime = "hh:mm:ss tt"
            EndIf
    EndSwitch
    ; Format DATE
    If $sTempDate <> "" Then
        $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_SDATE) ; Get short date format.
        If Not @error And Not ($sTempString = '') Then
            $sTempDate = StringReplace($sTempDate, "/", $sTempString)
        EndIf
        Local $iWday = _DateToDayOfWeek($asDatePart[1], $asDatePart[2], $asDatePart[3])
        $asDatePart[3] = StringRight("0" & $asDatePart[3], 2) ; make sure the length is 2
        $asDatePart[2] = StringRight("0" & $asDatePart[2], 2) ; make sure the length is 2
        $sTempDate = StringReplace($sTempDate, "d", "@")
        $sTempDate = StringReplace($sTempDate, "m", "#")
        $sTempDate = StringReplace($sTempDate, "y", "&")
        $sTempDate = StringReplace($sTempDate, "@@@@", _DateDayOfWeek($iWday, 0))
        $sTempDate = StringReplace($sTempDate, "@@@", _DateDayOfWeek($iWday, 1))
        $sTempDate = StringReplace($sTempDate, "@@", $asDatePart[3])
        $sTempDate = StringReplace($sTempDate, "@", StringReplace(StringLeft($asDatePart[3], 1), "0", "") & StringRight($asDatePart[3], 1))
        $sTempDate = StringReplace($sTempDate, "####", _DateToMonth($asDatePart[2], 0))
        $sTempDate = StringReplace($sTempDate, "###", _DateToMonth($asDatePart[2], 1))
        $sTempDate = StringReplace($sTempDate, "##", $asDatePart[2])
        $sTempDate = StringReplace($sTempDate, "#", StringReplace(StringLeft($asDatePart[2], 1), "0", "") & StringRight($asDatePart[2], 1))
        $sTempDate = StringReplace($sTempDate, "&&&&", $asDatePart[1])
        $sTempDate = StringReplace($sTempDate, "&&", StringRight($asDatePart[1], 2))
    EndIf
    ; Format TIME
    If $sTempTime <> "" Then
        $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_S1159) ; AM designator.
        If Not @error And Not ($sTempString = '') Then
            $sAM = $sTempString
        Else
            $sAM = "AM"
        EndIf
        $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_S2359) ; PM designator.
        If Not @error And Not ($sTempString = '') Then
            $sPM = $sTempString
        Else
            $sPM = "PM"
        EndIf
        $sTempString = _WinAPI_GetLocaleInfo($LOCALE_USER_DEFAULT, $LOCALE_STIME) ; Time seperator.
        If Not @error And Not ($sTempString = '') Then
            $sTempTime = StringReplace($sTempTime, ":", $sTempString)
        EndIf

        If (StringInStr($sDate, 'pm') > 0) Or (StringInStr($sDate, $sPM) > 0) Then
            If $asTimePart[1] < 12 Then $asTimePart[1] += 12
        ElseIf (StringInStr($sDate, 'am') > 0) Or (StringInStr($sDate, $sAM) > 0) Then
            If $asTimePart[1] = 12 Then $asTimePart[1] = 0
        EndIf

        If StringInStr($sTempTime, "tt") Then
            If $asTimePart[1] < 12 Then
                $sTempTime = StringReplace($sTempTime, "tt", $sAM)
                If $asTimePart[1] = 0 Then $asTimePart[1] = 12
            Else
                $sTempTime = StringReplace($sTempTime, "tt", $sPM)
                If $asTimePart[1] > 12 Then $asTimePart[1] = $asTimePart[1] - 12
            EndIf
        EndIf

        $asTimePart[1] = StringRight("0" & $asTimePart[1], 2) ; make sure the length is 2
        $asTimePart[2] = StringRight("0" & $asTimePart[2], 2) ; make sure the length is 2
        $asTimePart[3] = StringRight("0" & $asTimePart[3], 2) ; make sure the length is 2
        $sTempTime = StringReplace($sTempTime, "hh", StringFormat("%02d", $asTimePart[1]))
        $sTempTime = StringReplace($sTempTime, "h", StringReplace(StringLeft($asTimePart[1], 1), "0", "") & StringRight($asTimePart[1], 1))
        $sTempTime = StringReplace($sTempTime, "mm", StringFormat("%02d", $asTimePart[2]))
        $sTempTime = StringReplace($sTempTime, "ss", StringFormat("%02d", $asTimePart[3]))
        $sTempDate = StringStripWS($sTempDate & " " & $sTempTime, $STR_STRIPLEADING + $STR_STRIPTRAILING)
    EndIf
    Return $sTempDate
EndFunc   ;==>_DateTimeFormatEx

 

Edited by ioa747
simplify

I know that I know nothing

Posted

and a portable one, to be available  :)

ConsoleWrite("- " & _Time12hTo24h('08:70 PM') & @CRLF)    ; -2
ConsoleWrite("- " & _Time12hTo24h('08:30 PM') & @CRLF)    ; 20:30
ConsoleWrite("- " & _Time12hTo24h('12:00 AM') & @CRLF)    ; 00:00
ConsoleWrite("- " & _Time12hTo24h('12:00:44 PM') & @CRLF) ; 12:00:44
ConsoleWrite("- " & _Time12hTo24h('7:5 pm') & @CRLF)      ; 19:05
ConsoleWrite("- " & _Time12hTo24h('7:5 am') & @CRLF)      ; 07:05
ConsoleWrite("- " & _Time12hTo24h('08:30') & @CRLF)       ; 08:30


Func _Time12hTo24h($sTime)
    Local $sNewTime = StringStripWS($sTime, 8)
    Local Const $bPM = (StringInStr($sNewTime, 'pm') > 0)
    $sNewTime = StringReplace($sNewTime, "pm", "")
    $sNewTime = StringReplace($sNewTime, "am", "")
    Local $aTime = StringSplit($sNewTime, ':')
    If $aTime[0] < 2 Then Return SetError(1, 0, -1)
    Local $iHour, $iMin, $iSec
    $iHour = Number($aTime[1])
    $iMin = Number($aTime[2])
    If $iHour < 0 Or $iHour > 12 Then Return SetError(2, 0, -2)
    If $iMin < 0 Or $iMin > 59 Then Return SetError(2, 0, -2)
    If $bPM And $iHour < 12 Then $iHour += 12
    If Not $bPM And $iHour = 12 Then $iHour = 0
    $sNewTime = StringFormat("%02i:%02i", $iHour, $iMin)
    If $aTime[0] >= 3 Then
        $iSec = Number($aTime[3])
        If $iSec < 0 Or $iSec > 59 Then Return SetError(2, 0, -2)
        $sNewTime &= StringFormat(":%02i", $iSec)
    EndIf
    Return $sNewTime
EndFunc   ;==>_Time12hTo24h

 

I know that I know nothing

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...