Jump to content

Recommended Posts

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • 10 months later...
Posted

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?

  • Moderators
Posted

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:

  Reveal hidden contents

 

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

@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.

  • 3 months later...
Posted (edited)

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
  • Moderators
Posted (edited)

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:

  Reveal hidden contents

 

  • 2 years later...
Posted
#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:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 2 years later...
Posted

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?

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted
  On 4/29/2023 at 7:09 AM, 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

Expand  

 

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

thanks!

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted (edited)

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:

  Reveal hidden contents

 

  • 2 weeks later...
Posted

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)

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

works beautifully!

thank you Melba!

would this be considered an official release/replacement?

  • Moderators
Posted

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:

  Reveal hidden contents

 

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...