Jump to content

Recommended Posts

Posted (edited)

_DTFormat

Formats a given date/time string according to the specified format.

_DTFormat($sDate, $sFormat [, $iLcid = $LOCALE_USER_DEFAULT])

$sDate         - The date string to be formatted "[YYYY/MM/DD][ HH:MM:SS [ tt]]"
$sFormat    - A string containing the desired format for the date and time.
                               Supported tokens are: Date and/or Time
                                Date:
                                    d, dd = day; ddd, dddd = day of week; M= month; y = year
                                Time:
                                    h= hour m= minute
                                    s = second (long time only)
                                    tt= AM. or P.M.
                                    h/H = 12/24 hour
                                    hh, mm, ss = display leading zero
                                    h, m, s = do not display leading zero
                               Tokens can be separated by '|' to specify different formats for date and time.
$iLcid           -  [optional] The locale identifier. Defaults to the user's default locale (default is $LOCALE_USER_DEFAULT).

 

; https://www.autoitscript.com/forum/topic/213249-free-style-datetimeformat/
;----------------------------------------------------------------------------------------
; Title...........: _DTFormat.au3
; Description.....: Formats a given date/time string according to the specified format.
; AutoIt Version..: 3.3.16.1   Author: ioa747  Script Version: 0.1
; Note............: Testet in Win10 22H2       Date:07/10/2025
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <Date.au3>

Example() ; Function to demonstrate the use of _DTFormat

Func Example()
    ; https://help.tradestation.com/10_00/eng/tsdevhelp/elobject/class_el/lcid_values.htm
    ; Example using specific LCID (German=1031, English=1033, Spain=1034, France=1036)
    ConsoleWrite("-      German : " & _DTFormat("2025/09/01 08:30:00 PM", "dddd, d MMMM yyyy|, HH:mm", 1031) & @CRLF)
    ConsoleWrite("-     English : " & _DTFormat("2025/09/01 08:30:00 PM", "dddd, d MMMM yyyy|, HH:mm", 1033) & @CRLF)
    ConsoleWrite("-       Spain : " & _DTFormat("2025/09/01 08:30:00 PM", "dddd, d MMMM yyyy|, HH:mm", 1034) & @CRLF)
    ConsoleWrite("-      France : " & _DTFormat("2025/09/01 08:30:00 PM", "dddd, d MMMM yyyy|, HH:mm", 1036) & @CRLF)
    ConsoleWrite("-" & @CRLF)

    ; Example using the default User Locale LCID
    ConsoleWrite("- User Locale : " & _DTFormat("2025/09/01 20:30:00", "dddd, d MMMM yyyy|, hh:mm tt") & @CRLF)
    ConsoleWrite("-   MMMM yyyy : " & _DTFormat("2025/09/01", "MMMM yyyy") & @CRLF)
    ConsoleWrite("- Date & Time : " & _DTFormat("2025/09/01 20:30:00", "'Date:' dddd d|,  'Time:' HH:mm") & @CRLF)
    ConsoleWrite("-" & @CRLF)

    ; More Example
    ConsoleWrite("-   only Time : " & _DTFormat("08:30:00 PM", "HH:mm:ss") & @CRLF)
    ConsoleWrite("-   only Date : " & _DTFormat("2025/09/01", "dddd, d MMMM yyyy") & @CRLF)
    ConsoleWrite("-" & @CRLF)
    ConsoleWrite("-  _NowCalc() : " & _NowCalc() & @CRLF)
    ConsoleWrite("-   only Time : " & _DTFormat(_NowCalc(), "|HH:mm:ss") & @CRLF)
    ConsoleWrite("-   only Date : " & _DTFormat(_NowCalc(), "dddd, d MMMM yyyy|") & @CRLF)
    ConsoleWrite("-" & @CRLF)
    ConsoleWrite("- ! HH with tt: " & _DTFormat("08:30:00 PM", "HH:mm tt") & @CRLF)
    ConsoleWrite("-   Now Stamp : " & _DTFormat(_NowCalc(), "yyyy_MM_dd|_HH_mm_ss_" & @MSEC) & @CRLF)
    ConsoleWrite("-" & @CRLF)
EndFunc   ;==>Example

; #FUNCTION# ====================================================================================================================
; Name...........: _DTFormat
; Description....: Formats a given date/time string according to the specified format.
; Syntax.........: _DTFormat($sDate, $sFormat [, $iLcid = $LOCALE_USER_DEFAULT])
; Parameters.....: $sDate     - The date string to be formatted "[YYYY/MM/DD][ HH:MM:SS [ tt]]".
;                  $sFormat   - A string containing the desired format for the date and time.
;                               Supported tokens are: Date and/or Time
;                               Date:
;                                   d, dd = day; ddd, dddd = day of week; M= month; y = year
;                               Time:
;                                   h= hour m= minute
;                                   s = second (long time only)
;                                   tt= AM. or P.M.
;                                   h/H = 12/24 hour
;                                   hh, mm, ss = display leading zero
;                                   h, m, s = do not display leading zero
;                               Tokens can be separated by '|' to specify different formats for date and time.
;                  $iLcid     - [optional] The locale identifier. Defaults to the user's default locale (default is $LOCALE_USER_DEFAULT).
; Return values .: Success: Returns the formatted date string.
;                  Failure: Returns an empty string and set the @error flag to non-zero.
;                               @error:
;                                   1 - Error date is not valid.
;                                   2 - Error in splitting the date string
;                                   3 - Error in encoding SystemTime
;                                   4 - Error in time formatting
;                                   5 - Error in date formatting
; Author ........: ioa747
; Modified ......:
; Remarks .......: This function uses the Windows API to format date and time according to the specified locale.
; Related .......: _Date_Time_EncodeSystemTime, _WinAPI_GetDateFormat, _WinAPI_GetTimeFormat, _WinAPI_GetLocaleInfo
; Link ..........: https://learn.microsoft.com/en-us/windows/win32/intl/day--month--year--and-era-format-pictures
; Example .......: MsgBox(0, "Formatted Date", _DTFormat("2023/10/05 14:30:00", "MM/DD/YYYY|, HH:MM:SS"))
; ===============================================================================================================================
Func _DTFormat($sDate, $sFormat, $iLcid = $LOCALE_USER_DEFAULT)
    Local $asDatePart[4], $asTimePart[4]
    Local $sTempDate = "", $sTempTime = ""
    Local $sAM, $sPM, $sTempString = ""
    Local $bDate = True

    ; If there is no date, add a dummy one (2000/01/01)
    If StringInStr($sDate, "/") = 0 And Not @error Then
        $bDate = False
        $sDate = "2000/01/01 " & $sDate
    Else ; Verify If InputDate is valid
        If Not _DateIsValid($sDate) Then Return SetError(1, 0, "") ; Error date is not valid.
    EndIf

    ; Split the date and time into arrays
    _DateTimeSplit($sDate, $asDatePart, $asTimePart)
    If @error Then Return SetError(2, @error, "") ; Error in splitting the date string

    Local $aPart = StringSplit($sFormat, "|")

    If $bDate Then
        $sTempDate = $aPart[1]
        $sTempTime = ""
        If $aPart[0] = 2 Then $sTempTime = $aPart[2]
    Else
        $sTempTime = $aPart[1]
    EndIf

    ; If time parts exist, check for AM/PM and convert to 24-hour format
    If $asTimePart[0] > 1 Then

        ; Get locale's AM designator, or AM
        $sTempString = _WinAPI_GetLocaleInfo($iLcid, $LOCALE_S1159) ; AM designator.
        If Not @error And Not ($sTempString = '') Then
            $sAM = $sTempString
        Else
            $sAM = "AM"
        EndIf

        ; Get locale's PM designator, or PM
        $sTempString = _WinAPI_GetLocaleInfo($iLcid, $LOCALE_S2359) ; PM designator.
        If Not @error And Not ($sTempString = '') Then
            $sPM = $sTempString
        Else
            $sPM = "PM"
        EndIf

        ; Convert 12-hour clock (with PM) to 24-hour clock
        If (StringInStr($sDate, 'pm') > 0) Or (StringInStr($sDate, $sPM) > 0) Then
            If $asTimePart[1] < 12 Then $asTimePart[1] += 12
            ; Convert 12-hour clock (with AM) to 24-hour clock (handle 12 AM midnight case)
        ElseIf (StringInStr($sDate, 'am') > 0) Or (StringInStr($sDate, $sAM) > 0) Then
            If $asTimePart[1] = 12 Then $asTimePart[1] = 0
        EndIf
    EndIf

    ; Remove ' tt' if hour is in format H/24 hour
    If StringInStr($sTempTime, "H", 1) > 0 Then $sTempTime = StringReplace($sTempTime, " tt", "")

    ; Encode a system time structure (required by WinAPI date/time functions)
    Local $tSystem = _Date_Time_EncodeSystemTime($asDatePart[2], $asDatePart[3], $asDatePart[1], $asTimePart[1], $asTimePart[2], $asTimePart[3])
    If @error Then Return SetError(3, @error, "") ; Error in encoding SystemTime

    Local $sfinalTime = _WinAPI_GetTimeFormat($iLcid, $tSystem, 0, $sTempTime)
    If @error Then Return SetError(4, @error, "") ; Error in time formatting

    ; Force AM/PM if the format string contains 'tt' but regional settings didn't include it
    If StringInStr($sTempTime, "tt") Then
        If (StringInStr($sfinalTime, 'pm') = 0) And (StringInStr($sfinalTime, $sPM) = 0) And _
                (StringInStr($sfinalTime, 'am') = 0) And (StringInStr($sfinalTime, $sAM) = 0) Then
            If $asTimePart[1] < 12 Then
                $sfinalTime &= " " & $sAM
            Else
                $sfinalTime &= " " & $sPM
            EndIf
        EndIf
    EndIf

    Local $sResult = ""
    If $sTempDate <> "" Then $sResult &= _WinAPI_GetDateFormat($iLcid, $tSystem, 0, $sTempDate)
    If @error Then Return SetError(5, @error, "") ; Error in date formatting

    If $sTempTime <> "" Then $sResult &= $sfinalTime
    Return $sResult

EndFunc   ;==>_DTFormat

 

Please, every comment is appreciated!
leave your comments and experiences here!
Thank you very much  :)

 

Edited by ioa747

I know that I know nothing

Posted

Seems to work well, even with older Versions of AutoIt (in my case 3.3.14.0) 

-      German : Montag, 1 September 2025, 20:30
-     English : Monday, 1 September 2025, 20:30
-       Spain : lunes, 1 septiembre 2025, 20:30
-      France : lundi, 1 septembre 2025, 20:30
-
- User Locale : Montag, 1 September 2025, 08:30 PM
-   MMMM yyyy : September 2025
- Date & Time : Date: Montag 1,  Time: 20:30
-
-   only Time : 20:30:00
-   only Date : Montag, 1 September 2025
-
-  _NowCalc() : 2025/10/07 07:03:28
-   only Time : 07:03:28
-   only Date : Dienstag, 7 Oktober 2025
-
- ! HH with tt: 20:30
-   Now Stamp : 2025_10_07_07_03_28_825

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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
×
×
  • Create New...