Jump to content

Date_Time_Convert - New version 17 Jun 23


Melba23
 Share

Recommended Posts

  • Moderators

Thomymaster,

No - as I do not see this as of general use.. I suggest using StringRight to get the ms digits, StringTrimRight to remove them before conversion, and then concatenate the converted DTG with the stripped ms value. Or given the simplicity of the conversion just use a simple RegEx:

$sInput = "07/06/2015 15:02:04.234"

$sOutput = StringRegExpReplace($sInput, "^(\d\d)\/(\d\d)\/(.*)$", "$2.$1.$3")

ConsoleWrite($sOutput & " - Returned" & @CRLF & "06.07.2015 15:02:04.234 - Required" & @CRLF)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 10 months later...

not sure what's going on... this isn't working (using autoit v3.3.12)

but if i take out the "hh:mm" it works

#include <DTC.au3>
$date = "2015:11:16 10:14"

$month_short = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm", "MM")
$month_long = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm", "MMMM")
$year = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm", "yyyy")
Debug($date, $month_short, $month_long, $year)

any ideas?

Link to comment
Share on other sites

  • Moderators

gcue,

If you look at the @error/@extended values being returned and check the function header you will see that you have told the UDF that the hours are in 12-hour format but you have not specified AM/PM. If you change the hour format to 24-hour or specify AM/PM it works:

#include <DTC.au3>

$date = "2015:11:16 10:14"
; Use 24-hour format
$month_short = _Date_Time_Convert($date, "yyyy:MM:dd HH:mm", "MM")
$month_long = _Date_Time_Convert($date, "yyyy:MM:dd HH:mm", "MMMM")
$year = _Date_Time_Convert($date, "yyyy:MM:dd HH:mm", "yyyy")
ConsoleWrite($month_short & " - " & $month_long & " - " & $year & @CRLF)

$date = "2015:11:16 10:14 A"
; Specify AM/PM
$month_short = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm T", "MM")
$month_long = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm T", "MMMM")
$year = _Date_Time_Convert($date, "yyyy:MM:dd hh:mm T", "yyyy")
ConsoleWrite($month_short & " - " & $month_long & " - " & $year & @CRLF)

At times I wonder why I bother adding all the error messages and writing function headers......

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 10 months later...
  • 9 months later...

@Melba23Awesome, tried for 45 min to convert a string based on the $LOCALE_SSHORTDATE.

Found a different thread with your remark "My Date_Time_Convert UDF (look in my sig below for the link) will convert that string to any date format you require."

- bam - 1 minute later done. Thank you very much.

Link to comment
Share on other sites

  • 3 months later...

Hi @Melba23 ,

I was having some issues with automation of Logs wherein the Date Time is either in English or in some Locale based on the configured OS. It would be great if we come to know about the Locale and then the array of Month Abbreviation in the chosen language is  used/created. Would this be possible ?

I have been trying to find some list or anything that would help me in getting the Abbreviations but have hit the wall.

 

Local $German = '25 Mrz 2018'
Local $English = '25 Mar 2018'
ConsoleWrite(_Date_Time_Convert($German,'dd MMM yyyy','dd/MM/yyyy') & @CRLF) ;<-- Returns Empty
ConsoleWrite(_Date_Time_Convert($English,'dd MMM yyyy','dd/MM/yyyy') & @CRLF)

Regards

DR.

Edited by DeltaRocked
typo
Link to comment
Share on other sites

  • Moderators

DeltaRocked,

This script will get you the short month names for the currently set locale in the OS.

This code seems to do it for all locales, but I am not sufficiently C literate to convert it.

M23

Edit: Just found this script - looking into it now.

Edit 2: Here you go:

#include <WinAPILocale.au3>
#include <Array.au3>

Global $aLocales[10] = [0]
For $i = 0 To 0xFFFF
    ; Check if valid LCID
    $iLocale = _WinAPI_GetLocaleInfo($i, 0x01)
    If $iLocale Then
        ; Store LCID
        $aLocales[0] += 1
        ; Resize array as required
        If UBound($aLocales) <= $aLocales[0] Then
            ReDim $aLocales[UBound($aLocales) * 2]
        EndIf
        $aLocales[$aLocales[0]] = $iLocale
    EndIf
Next
; Remove any unused elements
ReDim $aLocales[$aLocales[0] + 1]

; just for display
Global $aShortMonths[$aLocales[0]][13]

; Loop though LCIDs
For $i = 1 To $aLocales[0]
    ; Convert to decimal
    $iLCID = Dec($aLocales[$i])
    ; Get language definition
    $aShortMonths[$i - 1][0] = _WinAPI_GetLocaleInfo($iLCID, 0x5C)
    ; Get short months
    For $j = 0 To 11
        $aShortMonths[$i - 1][$j + 1] = _WinAPI_GetLocaleInfo($iLCID, $LOCALE_SABBREVMONTHNAME1 + $j)
    Next
Next
; Sort on language
_ArraySort($aShortMonths)
; And here we have them!
_ArrayDisplay($aShortMonths, "", Default, 8)

Now if you know the language set for the log file you can easily find out the short months used.

Edit 3: The @OSLang & @KBLayout macros could help you determine the language.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 years later...
#include <MsgBoxConstants.au3>

#include "..\DTC.au3"

Global $sIn_Date, $sOut_Date

$sIn_Date = "20190411183015"
$sOut_Date = _Date_Time_Convert($sIn_Date, "yyyyMMddHHmmss", "yyyy-MM-dd hh:mm:ss")
MsgBox($MB_SYSTEMMODAL, "DTC Conversion (for MS SQL DateTime format)", $sIn_Date & @CRLF & $sOut_Date)

Please consider to add this to examples to the UDF .

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 2 years later...

love this UDF.  been using it for years.  noticing something strange not sure if its expected?  the first 2 do not work but the last 2 work

#include "DTC.au3"

;~ $string = "4/28/2023 3:19"
$string = "4/28/2023 9:51"
;~ $string = "4/28/2023 10:51"
;~ $string = "4/28/2023 14:51"

$new_string = _Date_Time_Convert($string, "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

the second string outputs

202304289:1

the first string outputs

202304283:9

while the other 2 output what i would expect

202304281051
202304281451

 

i know it has to do with the hour only having 1 digit.  the dataset im working with doesnt include 0 when the hour is just 1 digit 

thoughts?

Link to comment
Share on other sites

  • Moderators

gcue,

The problem is that the "in" hour is in a most unusual "unpadded 24hr" format, which is not one that the UDF is expecting. The formats currently accepted are:

Hour   : HH = 20 (2 digit 24 hr)
         hh = 08 (2 digit padded 12 hr)
         h = 8 (1/2 digit 12 hr)

I will look into the code to see if I can add another format to the UDF to cover your case.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

19 minutes ago, Melba23 said:

gcue,

The problem is that the "in" hour is in a most unusual "unpadded 24hr" format, which is not one that the UDF is expecting. The formats currently accepted are:

Hour   : HH = 20 (2 digit 24 hr)
         hh = 08 (2 digit padded 12 hr)
         h = 8 (1/2 digit 12 hr)

I will look into the code to see if I can add another format to the UDF to cover your case.

M23

 

right strange thing is id expect to see 09 especially because other cases in the same data set are HH formatted - unfortunately no😕

thanks!

Link to comment
Share on other sites

  • Moderators

gcue,

As a workaround, you can add the padding yourself - this works foe me:

#include "DTC.au3"

$string = "4/28/2023 3:19"
$new_string = _Date_Time_Convert( _PadHour($string), "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

$string = "4/28/2023 9:51"
$new_string = _Date_Time_Convert( _PadHour($string), "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

$string = "4/28/2023 10:51"
$new_string = _Date_Time_Convert( _PadHour($string), "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

$string = "4/28/2023 14:51"
$new_string = _Date_Time_Convert( _PadHour($string), "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

Func _PadHour($sString)

    ; Extract hour value
    $sHour = StringRegExpReplace($sString, "^.*\s(.{1,2}):.*$","$1")
    ; If only a single digit
    If StringLen($sHour ) = 1 Then
        ; Add padding
        $sString = StringRegExpReplace($sString, "\s.*:", " 0" & $sHour & ":")
    EndIf
    Return $sString

EndFunc

But I will keep looking at the UDF internal code to see if I can incorporate this format.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

gcue,

That was harder than I thought it would be, but I think I have got there in the end. Please try this new version of the UDF which will accept  a single "H" in the mask to deal with a non-padded 24hr format: DTC_H.au3

Eagerly awaiting confirmation that my rewrite of the "Hour" section of the UDF actually works in the wild!

M23

Edited by Melba23
Added a error check missed in the rewrite

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 weeks later...

haha "in the wild"

the first 2 dont work but the last 2 do... same as before i think :)

#include "dtc_h.au3"

$string = "4/28/2023 3:19"
;~ $string = "4/28/2023 9:51"
;~ $string = "4/28/2023 10:51"
;~ $string = "4/28/2023 14:51"

$new_string = _Date_Time_Convert($string, "M/d/yyyy HH:mm", "yyyyMMddHHmm")
ConsoleWrite($new_string & @CRLF)

 

Link to comment
Share on other sites

  • Moderators

gcue,

Sorry for the late reply - my PC was in the repair shop.

You are still using the HH mask for the input - fairly obviously that will produce the same error as before. The whole point of the UDF rewrite was to allow you to use the H mask - unpadded 24hr values. So change the input mask to read "M/d/yyyy H:mm" and all should be well.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

gcue,

Delighted that it works for you. I will try to release a new version shortly, but I have my entire family with me at the moment and time is a bit difficult to find!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Melba23 changed the title to Date_Time_Convert - New version 17 Jun 23

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
 Share

  • Recently Browsing   0 members

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