Jump to content

extracting dates from different file name formats


Recommended Posts

hello world

Using UEZ's example (thanks again UEZ) I have been able to extract several dates from some file name formats. I am having trouble with strings that have numbers jarbled next to each other without distinguishable seperators that isolate yyyy,mm,dd,hh,mm etc

here is an example:

$date = "20141119_193702"

i am trying to get it into this format 11/19/2014 07:37 PM

here is what i have tried:

Func FormatDate($date)

    $aDate = StringRegExp($date, "(?i)(\d){4}(\d){2}(\d){2}\_(\d){2}(\d){2}(\d){2}", 3)

    If @error Then
        Return SetError(1)
    EndIf
debug($aDate)
    if $aDate[3] > 12 Then
        $hour = $aDate[3] - 12

        if StringLen($hour) = 1 then
            $hour = "0" & $hour
        EndIf

        $tt = "PM"
    Else
        $hour = $aDate[3]
        $tt = "AM"
    EndIf

    $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

    Return $date_formatted

EndFunc   ;==>FormatDate

also, as mentioned i have been able to resolve several formats so how can i use the same function to go through several stringregexp patterns?

for instance here are some of the formats ive resolved and below are the corresponding stringregexp patterns ive used,  how can i consolidate all the functions into 1?

;A
$date = "2012-12-30_14-48-34_90"                        ;12/30/2012 02:48 PM

;B
;~ $date = "11-08-1988"                                     ;11/08/1988 12:00 AM

;C
;~ $date = "12-26-2012-bridge(1)"                           ;12/26/2012 12:00 AM

;D
;~ $date = "2015-04-29 03.46.36"                            ;04/29/2015 03:46 AM


$date_formatted = FormatDate($date)

If @error Then
    MsgBox(0, "Unable to Format Date", $date)
    Exit
EndIf

debug($date_formatted)


;A
;~ $date = "2012-12-30_14-48-34_90"                         ;12/30/2012 02:48 PM
;~ Func FormatDate($date)

;~  $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\_(\d+)-(\d+)\-(\d+)\_(\d+)", 3)

;~  If @error Then
;~      Return SetError(1)
;~  EndIf

;~  if $aDate[3] > 12 Then
;~      $hour = $aDate[3] - 12

;~      if StringLen($hour) = 1 then
;~          $hour = "0" & $hour
;~      EndIf

;~      $tt = "PM"
;~  Else
;~      $hour = $aDate
;~      $tt = "AM"
;~  EndIf

;~  $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

;~  Return $date_formatted

;~ EndFunc   ;==>FormatDate


;B
;~ $date = "11-08-1988"                                     ;11/08/1988 12:00 AM
;~ Func FormatDate($date)

;~  $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)", 3)

;~  If @error Then
;~      Return SetError(1)
;~  EndIf

;~  $date_formatted = StringFormat("%02i/%02i/%04i", $aDate[0], $aDate[1], $aDate[2]) & " 12:00 AM"

;~  Return $date_formatted

;~ EndFunc   ;==>FormatDate


;C
;~ $date = "12-26-2012-bridge(1)"                           ;12/26/2012 12:00 AM
;~ Func FormatDate($date)

;~  $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-(\w+)", 3)

;~  If @error Then
;~      Return SetError(1)
;~  EndIf

;~  $date_formatted = StringFormat("%02i/%02i/%04i", $aDate[0], $aDate[1], $aDate[2]) & " 12:00 AM"

;~  Return $date_formatted

;~ EndFunc   ;==>FormatDate


;D
;~ $date = "2015-04-29 03.46.36"                            ;04/29/2015 03:46 AM
;~ Func FormatDate($date)

;~  $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\h+(\d+)\.(\d+)\.(\d+)", 3)

;~  If @error Then
;~      Return SetError(1)
;~  EndIf

;~  if $aDate[3] > 12 Then
;~      $hour = $aDate[3] - 12

;~      if StringLen($hour) = 1 then
;~          $hour = "0" & $hour
;~      EndIf

;~      $tt = "PM"
;~  Else
;~      $hour = $aDate[3]
;~      $tt = "AM"
;~  EndIf

;~  $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

;~  Return $date_formatted

;~ EndFunc   ;==>FormatDate

Thank you in advance!

Link to comment
Share on other sites

$date = "20141119_193702"

$sDate = stringmid($date , 5 , 2) & "/" & stringmid($date , 7 , 2) & "/" & stringleft($date , 4)
$sTime = stringmid($date , 10 , 2) > 12 ? stringmid($date , 10 , 2) - 12 & ":" & stringmid($date , 12 , 2) & " PM" : stringmid($date , 10 , 2) & ":" & stringmid($date , 12 , 2) & "AM"
$sDateTime = $sDate & " " & $sTime

msgbox(0, '' , $sDateTime)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

more concise than two lines, and handling more than all dates formatted as " YYYYMMDD (any separator) HHMMSS. " ?  

  I would still use a regexp to ensure the string should be run through those two lines, but the regexp would be intentionally ugly so someone would come along and improve it, like this:

 

IF stringregexp($date , "\A\d\d\d\d\d\d\d\d.\d\d\d\d\d\d\z") Then
$sTime = stringmid($date , 10 , 2) > 12 ? stringmid($date , 10 , 2) - 12 & ":" & stringmid($date , 12 , 2) & " PM" : stringmid($date , 10 , 2) & ":" & stringmid($date , 12 , 2) & "AM"
$sDateTime = stringmid($date , 5 , 2) & "/" & stringmid($date , 7 , 2) & "/" & stringleft($date , 4) & " " & $sTime
EndIf

 

and i would like to see a race since the regexp would have to have the math built in (which would be some nice regexp I would like to see).

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

yes i am familiar with that wonderful UDF.  however i wanted the stringregexp equivalent because i have strings with these formats:

$filename = "IMG00136-20100524-0109"                            ;05/24/2010 01:09 AM
;~ $filename = "IMG_20000526_100019_402"                        ;05/26/2000 10:00 AM
;~ $filename = "IMG-20120615-00028"                                ;06/15/2012 12:00 AM
;~ $filename = "Texas-20111117-00060"                            ;11/17/2011    12:00 AM
;~ $filename = "Southwest San Marcos Valley-20111110-00046"        ;11/10/2011 12:00 AM
;~ $filename = "Long Island-Laketown-20110526-00023"            ;05/26/2011 12:00 AM

i was able to resolve the top example with this:

#include <array.au3>

$msg_normal = 0

$filename = "20141119_193702"                               ;11/19/2014 07:37 PM

$date_formatted = FormatDate($date)

If @error Then
    MsgBox(0, "Unable to Format Date", $date)
    Exit
EndIf

debug($date_formatted)

Func FormatDate($date)

    $aDate = StringRegExp($date, "(?i)(\d{4})(\d{2})(\d{2})\_(\d{2})(\d{2})(\d{2})", 3)

    If @error Then
        Return SetError(1)
    EndIf

    if $aDate[3] > 12 Then
        $hour = $aDate[3] - 12

        if StringLen($hour) = 1 then
            $hour = "0" & $hour
        EndIf

        $tt = "PM"
    Else
        $hour = $aDate[3]
        $tt = "AM"
    EndIf

    $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

    Return $date_formatted

EndFunc   ;==>FormatDate


Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

i am just having trouble seperating the number values into array rows of YYYY, MM, DD, HH, MM

 

 

Edited by gcue
Link to comment
Share on other sites

i followed your advice AdamUL and i was able to resolve all file names to the correct date/time format.

i am still wondering how I can consolidate all methods of resolving (file names > date stamps) into one function.  i am going to process a list of file names and am not sure how to automatically process and figure out which SRE or Date_Time_Convert method to use.

any help would greatly be appreciated!

Link to comment
Share on other sites

I still imagine a Function full of If statements or cases

If  StringRegExp($date, "(?i)(\d{4})(\d{2})(\d{2})\_(\d{2})(\d{2})(\d{2})" Then

$aDate = StringRegExp($date, "(?i)(\d{4})(\d{2})(\d{2})\_(\d{2})(\d{2})(\d{2})", 3)

...

ElseIf StringRegExp($date , " Some other pattern")

$aDate = StringRegExp($date, "Some other pattern", 3)

...

EndIf

 

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

cool concept thanks for your help - i just tried it but for some reason its not working...none of the $date examples use the right ELSEIF 

#include <array.au3>
#include <DTC.au3>

$msg_normal = 0


;A
;~ $date = "2/3/2012 8:38 PM"
;~ $date = "2/03/2012 08:38 PM"
;~ $date = "02/3/2012 8:38 AM"
;~ $date = "11/03/2012 8:38 AM"

;~ B;~$date = "2012-12-30_14-48-34_90"                          ;12/30/2012 02:48 PM
;~ C;~$date = "11-08-1988"                                      ;11/08/1988 12:00 AM
;~ D;~ $date = "12-26-2012-bridge(1)"                           ;12/26/2012 12:00 AM
;~ E;~ $date = "2015-04-29 03.46.36"                            ;04/29/2015 03:46 AM
;~ F;~ $date = "1996"                                           ;01/01/1996 12:00 AM
;~ G;~ $date = "College 1996"                                   ;01/01/1996 12:00 AM
;~ H;~ $date = "20141119_193702"                                ;11/19/2014 07:37 PM
;~ I;~ $date = "IMG00136-20100524-0109"                         ;05/24/2010 01:09 AM
;~ J;~ $date = "IMG_20000526_100019_402"                        ;05/26/2000 10:00 AM
;~ K;~ $date = "IMG-20120615-00028"                             ;06/15/2012 12:02 AM
;~ L;~ $date = "Texas-20111117-00060"                           ;11/17/2011 12:06 AM
;~ M;~ $date = "Southwest San Marcos Valley-20111110-00046"     ;11/10/2011 12:02 AM
;~ N;~ $date = "Long Island-Laketown-20110526-00023"            ;05/26/2011 12:02 AM

$date_formatted = FormatDate($date)

If @error Then
    MsgBox(0, "Unable to Format Date", $date)
    Exit
EndIf

Debug($date_formatted)


Func FormatDate($date)

    If StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3) Then
        Debug("A")
        $aDate = StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\_(\d+)-(\d+)\-(\d+)\_(\d+)", 3) Then
        Debug("B")
        $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\_(\d+)-(\d+)\-(\d+)\_(\d+)", 3)

        If $aDate[3] > 12 Then
            $hour = $aDate[3] - 12

            If StringLen($hour) = 1 Then
                $hour = "0" & $hour
            EndIf

            $tt = "PM"
        Else
            $hour = $aDate
            $tt = "AM"
        EndIf

        $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)", 3) Then
        Debug("C")
        $date_formatted = _Date_Time_Convert($date, "MM-dd-yyyy", "MM/dd/yyyy") & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-(\w+)", 3) Then
        Debug("D")
        $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-(\w+)", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", $aDate[0], $aDate[1], $aDate[2]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-h+(\d+)\.(\d+)\.(\d+)", 3) Then
        Debug("E")

        $date_formatted = _Date_Time_Convert($date, "yyyy-MM-dd HH.mm.ss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\d{4})", 3) Then
        Debug("F")
        $aDate = StringRegExp($date, "(?i)(\d{4})", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", "01", "01", $aDate[0]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d{4})", 3) Then
        Debug("G")
        $aDate = StringRegExp($date, "(?i)(\d{4})", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", "01", "01", $aDate[0]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\_(\d+)", 3) Then
        Debug("H")
        $date_formatted = _Date_Time_Convert($date, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w{3})(\d+)\-(\d+)\-(\d+)", 3) Then
        Debug("I")
        $aDate = StringRegExp($date, "(?i)(\w{3})(\d+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[2] & "_" & $aDate[3]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w{3})\_(\d+)\_(\d+)\_(\d+)", 3) Then
        Debug("J")
        $aDate = StringRegExp($date, "(?i)(\w{3})\_(\d+)\_(\d+)\_(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3) Then
        Debug("K")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3) Then
        Debug("L")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3) Then
        Debug("M")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\w+)\-(\d+)\-(\d+)", 3) Then
        Debug("N")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[2] & "_" & $aDate[3]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")
    EndIf

    Return $date_formatted

EndFunc   ;==>FormatDate

Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

here to make this script runable:

https://www.autoitscript.com/forum/topic/154684-date_time_convert-bugfix-version-11-jan-14/?do=findComment&comment=1116938

any help would be much much appreciated!

Edited by gcue
Link to comment
Share on other sites

this seems to work better... not quite there yet tho =)

 

#include <array.au3>
#include <DTC.au3>

$msg_normal = 0


;A
;~ $date = "2/3/2012 8:38 PM"
;~ $date = "2/03/2012 08:38 PM"
;~ $date = "02/3/2012 8:38 AM"
;~ $date = "11/03/2012 8:38 AM"

;~ B;~$date = "2012-12-30_14-48-34_90"                          ;12/30/2012 02:48 PM
;~ C;~$date = "11-08-1988"                                      ;11/08/1988 12:00 AM
;~ D;~$date = "12-26-2012-bridge(1)"                            ;12/26/2012 12:00 AM
;~ E;~$date = "2015-04-29 03.46.36"                             ;04/29/2015 03:46 AM
;~ F;~$date = "1996"                                            ;01/01/1996 12:00 AM
;~ G;~$date = "College 1996"                                    ;01/01/1996 12:00 AM
;~ H;~$date = "20141119_193702"                                 ;11/19/2014 07:37 PM
;~ I;~$date = "IMG00136-20100524-0109"                          ;05/24/2010 01:09 AM
;~ J;~$date = "IMG_20000526_100019_402"                         ;05/26/2000 10:00 AM
;~ K;~$date = "IMG-20120615-00028"                              ;06/15/2012 12:02 AM
;~ L;~$date = "Texas-20111117-00060"                            ;11/17/2011 12:06 AM
;~ M;~$date = "Southwest San Marcos Valley-20111110-00046"      ;11/10/2011 12:02 AM
;~ N;~$date = "Long Island-Laketown-20110526-00023"             ;05/26/2011 12:02 AM

$date_formatted = FormatDate($date)

If @error Then
    MsgBox(0, "Unable to Format Date", $date)
    Exit
EndIf

Debug($date_formatted)


Func FormatDate($date)

    Local $date_formatted

    If StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3) <> @error   Then
        Debug("A")
        $aDate = StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\_(\d+)-(\d+)\-(\d+)\_(\d+)", 3) <> @error  Then
        Debug("B")
        $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\_(\d+)-(\d+)\-(\d+)\_(\d+)", 3)

        If $aDate[3] > 12 Then
            $hour = $aDate[3] - 12

            If StringLen($hour) = 1 Then
                $hour = "0" & $hour
            EndIf

            $tt = "PM"
        Else
            $hour = $aDate
            $tt = "AM"
        EndIf

        $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[1], $aDate[2], $aDate[0], $hour, $aDate[4], $tt)

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)", 3) <> @error Then
        Debug("C")
        $date_formatted = _Date_Time_Convert($date, "MM-dd-yyyy", "MM/dd/yyyy") & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-(\w+)", 3)  <> @error  Then
        Debug("D")
        $aDate = StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-(\w+)", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", $aDate[0], $aDate[1], $aDate[2]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\-(\d+)\-(\d+)\-h+(\d+)\.(\d+)\.(\d+)", 3) <> @error Then
        Debug("E")

        $date_formatted = _Date_Time_Convert($date, "yyyy-MM-dd HH.mm.ss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\d{4})", 3) <> @error Then
        Debug("F")
        $aDate = StringRegExp($date, "(?i)(\d{4})", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", "01", "01", $aDate[0]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d{4})", 3) <> @error   Then
        Debug("G")
        $aDate = StringRegExp($date, "(?i)(\d{4})", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i", "01", "01", $aDate[0]) & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\d+)\_(\d+)", 3) <> @error   Then
        Debug("H")
        $date_formatted = _Date_Time_Convert($date, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w{3})(\d+)\-(\d+)\-(\d+)", 3)  <> @error  Then
        Debug("I")
        $aDate = StringRegExp($date, "(?i)(\w{3})(\d+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[2] & "_" & $aDate[3]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w{3})\_(\d+)\_(\d+)\_(\d+)", 3)  <> @error  Then
        Debug("J")
        $aDate = StringRegExp($date, "(?i)(\w{3})\_(\d+)\_(\d+)\_(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)  <> @error  Then
        Debug("K")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)  <> @error  Then
        Debug("L")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)  <> @error  Then
        Debug("M")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\w+)\-(\w+)\-(\d+)\-(\d+)", 3)  <> @error  Then
        Debug("N")
        $aDate = StringRegExp($date, "(?i)(\w+)\-(\w+)\-(\d+)\-(\d+)", 3)

        $stamp = $aDate[2] & "_" & $aDate[3]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")
    EndIf

    If $date_formatted = "" Then
        Return SetError(1)
    EndIf

    Return $date_formatted

EndFunc   ;==>FormatDate

Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

 

Link to comment
Share on other sites

cases like 'F' are not strict enough,  the string for 'C' matches the regexp for 'F'.   Eventhough that example would work as it would hit C first, I would put the least strict ones all the way at the bottom to catch anything that falls through.  And looks like one of the items was pasted multiple times, two in a row ElseIf for:

(?i)(\w+)\-(\d+)\-(\d+)", 3)
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

i am still working on trying to resolve dates from the "unresolved" section - any help is deeply appreciated on that part! :)

my question is about how to avoid things from matching... the "should not match" values are matching my patterns and awful dates are resulting.

many many thanks in advance!

#include <array.au3>
#include <DTC.au3>

$msg_normal = 0

Func dates_array()

    ;0 - file_name
    ;1 - resolved date
    ;2 - method used

    Local $array[65][3]

    ;resolved
    $array[0][0] = "2/3/2012 8:38 PM"
    $array[1][0] = "2/03/2012 08:38 PM"
    $array[2][0] = "02/3/2012 8:38 AM"
    $array[3][0] = "11/03/2012 8:38 AM"
    $array[4][0] = "11/03/2012 08:38 AM"
    $array[5][0] = "2012-12-30_14-48-34_90"
    $array[6][0] = "2012_12_30_14_48_34_90"
    $array[7][0] = "2012-12-30-14-48-34-90"
    $array[8][0] = "2012-12-30 14-48-34-90"
    $array[9][0] = "2015-04-29 03.46.36"
    $array[10][0] = "2015_04_29 03.46.36"
    $array[11][0] = "12-26-2012-bridge(1)"
    $array[12][0] = "12_26_2012-bridge(1)"
    $array[13][0] = "12-26-2012"
    $array[14][0] = "12_26_2012"
    $array[15][0] = "IMG00136-20100524-0109"
    $array[16][0] = "IMG00136_20100524_0109"
    $array[17][0] = "IMG_20000526_100019_402"
    $array[18][0] = "IMG-20120615-00028"
    $array[19][0] = "IMG_20120615_00028"
    $array[20][0] = "Texas-20111117-00060"
    $array[21][0] = "Texas_20111117_00060"
    $array[22][0] = "Southwest San Marcos Valley-20111110-00046"
    $array[23][0] = "Southwest San Marcos Valley_20111110_00046"
    $array[24][0] = "Long Island-Laketown-20110526-00023"
    $array[25][0] = "Long Island-Laketown_20110526_00023"
    $array[26][0] = "20141119_193702"
    $array[27][0] = "20141119-193702"

    ;still need to resolve
    $array[28][0] = "2014071495201859"
    $array[29][0] = "2013072695195930"
    $array[30][0] = "IMG-20140619-WA0000"
    $array[31][0] = "IMG-20140402-WA0000"
    $array[32][0] = "VID-20141002-WA0001"
    $array[33][0] = "VID-20141009-WA0004"
    $array[34][0] = "IMG95201405169510533295434"
    $array[35][0] = "IMG95201310319519475695780"
    $array[36][0] = "IMG952014050695205100"
    $array[37][0] = "IMG952013010695192927"
    $array[38][0] = "Resampled952012-07-099515-09-279577"
    $array[39][0] = "Resampled952012-05-169519-32-049577"
    $array[40][0] = "Resampled952012-05-129518-02-1795365"
    $array[41][0] = "Resampled952012-06-109513-34-0395360"
    $array[42][0] = "IMG_20141003_244125_273"
    $array[43][0] = "IMG_20141003_244129_571"
    $array[44][0] = "2012-07-149519"
    $array[45][0] = "VID_20120415103537718"
    $array[46][0] = "VID_20120415103537718"
    $array[47][0] = "VN_20120520103037802"
    $array[48][0] = "VN_20121005215040254"
    $array[49][0] = "PicStory-2012-04-01-02-53"
    $array[50][0] = "2012-12-209510-42-3195121"
    $array[51][0] = "2012-12-219512-05-0395507"
    $array[52][0] = "2014-08-259507.27.29"
    $array[53][0] = "2013-01-29"

    ;should not match
    $array[54][0] = "0623112010"
    $array[55][0] = "0710122020"
    $array[56][0] = "0710122022"
    $array[57][0] = "0710122024"
    $array[58][0] = "0710122026"
    $array[59][0] = "0710122020"
    $array[60][0] = "0710122022"
    $array[61][0] = "0710122023a"
    $array[62][0] = "0710122024"
    $array[63][0] = "0710122026"
    $array[64][0] = "13659097338151"

    Return $array

EndFunc   ;==>dates_array

Global $count_a = 0
Global $count_b = 0
Global $count_c = 0
Global $count_d = 0
Global $count_e = 0
Global $count_f = 0
Global $count_g = 0
Global $count_h = 0
Global $count_i = 0
Global $count_j = 0
Global $count_k = 0
Global $count_l = 0
Global $count_m = 0

$dates_array = dates_array()

For $x = 0 To UBound($dates_array) - 1
    $date_formatted = _GetFileNameDate($dates_array[$x][0])

    If @error Then
        $dates_array[$x][2] = "ERROR"
    Else
        $array = StringSplit($date_formatted, "|")

        $resolved_date = $array[1]
        $method = $array[2]

        $dates_array[$x][1] = $resolved_date
        $dates_array[$x][2] = $method
    EndIf
Next

ConsoleWrite("Count A: " & $count_a & @CRLF)
ConsoleWrite("Count B: " & $count_b & @CRLF)
ConsoleWrite("Count C: " & $count_c & @CRLF)
ConsoleWrite("Count D: " & $count_d & @CRLF)
ConsoleWrite("Count E: " & $count_e & @CRLF)
ConsoleWrite("Count F: " & $count_f & @CRLF)
ConsoleWrite("Count G: " & $count_g & @CRLF)
ConsoleWrite("Count H: " & $count_h & @CRLF)
ConsoleWrite("Count I: " & $count_i & @CRLF)
ConsoleWrite("Count J: " & $count_j & @CRLF)
ConsoleWrite("Count K: " & $count_k & @CRLF)
ConsoleWrite("Count L: " & $count_l & @CRLF)
ConsoleWrite("Count M: " & $count_m & @CRLF)

Debug($dates_array)


Func _GetFileNameDate($date)

    Local $date_formatted

    If StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3) <> @error Then
        $method = "A" ;;;;;;;;;;;;;;;;;;;;temp
        $count_a += 1 ;;;;;;;;;;;;;;;;;;;;;;;temp

        $aDate = StringRegExp($date, "(?i)(\d+)\/(\d+)\/(\d+)\h+(\d+):(\d+)\h+(\w+)", 3)

        $date_formatted = StringFormat("%02i/%02i/%04i %02i:%02i %s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

    ElseIf StringRegExp($date, "(?i)(\d{4})\V(\d{2})\V(\d{2})\V(\d{2})\V(\d{2})\V(\d{2})", 3) <> @error Then
        $method = "B" ;;;;;;;;;;;;;;;;;;;;temp
        $count_b += 1 ;;;;;;;;;;;;;;;;;;;;;;;temp

        $aDate = StringRegExp($date, "(?i)(\d{4})\V(\d{2})\V(\d{2})\V(\d{2})\V(\d{2})\V(\d{2})", 3)

        $stamp = $aDate[0] & $aDate[1] & $aDate[2] & "_" & $aDate[3] & $aDate[4] & $aDate[5]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")

    ElseIf StringRegExp($date, "(?i)(\A)(\d{2})\V(\d{2})\V(\d{4})", 3) <> @error Then
        $method = "C" ;;;;;;;;;;;;;;;;;;;;temp
        $count_c += 1 ;;;;;;;;;;;;;;;;;;;;;;;temp

        $date_formatted = _Date_Time_Convert($date, "MM-dd-yyyy", "MM/dd/yyyy") & " 12:00 AM"

    ElseIf StringRegExp($date, "(?i)(\V*)(\d{8})\V(\d{4})", 3) <> @error Then
        $method = "D" ;;;;;;;;;;;;;;;;;;;;temp
        $count_d += 1 ;;;;;;;;;;;;;;;;;;;;;;;temp

        $aDate = StringRegExp($date, "(?i)(\V*)(\d{8})\V(\d{4})", 3)

        $stamp = $aDate[1] & "_" & $aDate[2]

        $date_formatted = _Date_Time_Convert($stamp, "yyyyMMdd_HHmmss", "MM/dd/yyyy hh:mm TT")



    EndIf

    If $date_formatted = "" Then
        Return SetError(1)
    EndIf

    Return $date_formatted & "|" & $method

EndFunc   ;==>_GetFileNameDate

Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

here is Melba's _Date_Time_Convert.au3 (DTC.au3) UDF to make the script run-able

https://www.autoitscript.com/forum/topic/154684-date_time_convert-bugfix-version-11-jan-14/?do=findComment&comment=1116938

 

Link to comment
Share on other sites

so here's an example where i get a string of numbers that might/might not be date...how can i validate if its a real date?  the strings i get come in different # of digits unfortunately

#include <array.au3>
#include <string.au3>
#include <DTC.au3>

$msg_normal = 0

$string = "2348012348"
;~ $string = "2014071495201859"

$date = StringLeft($string, 8)

If StringIsDigit($date) = 1 Then
    $date = $date & " 12:00 AM"
    $date_formatted = _Date_Time_Convert($date, "yyyyMMdd hh:mm TT", "MM/dd/yyyy hh:mm TT")
    Debug($date_formatted)
EndIf


Func Debug($variable1 = "", $variable2 = "", $variable3 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

 

Edited by gcue
Link to comment
Share on other sites

When you say 'should not match' I don't know the reason why. What defines a match:

0623112010 could be 11/20/0623 although that date is probably ambiguous. Is there an earliest or a latest acceptable date?

Edit

The above example could also indicate 11/23/2010.

Edited by czardas
Link to comment
Share on other sites

I don't think it's a date because the other values below it aren't dates (if we use the same sequence).  To validate a legitimate date.. Can't be a date in the future.  The past is harder to validate.  I guess no more than  70 years. 

 

Thanks again for your help! 

Edited by gcue
Link to comment
Share on other sites

That time window helps. With something like this I don't believe it is possible to generate a foolproof method. All the formats you intend to cover need to be defined. I think it's best to start with the smallest: I see you have YYYY (only) as one example. It may help to look at this wiki page: http://en.wikipedia.org/wiki/Date_format_by_country There you will see that there are many date variants used worldwide. Many may not be so significant, but it will give you a starting point.

Once the exact formats you want to include have been identified, then it is a question of seeding out the formats which require disambiguation. When no clear interpretaion can be deduced, the question arises what to do in such a situation. Imagine a two digit year 03/01/02, probably you should ignore it, but you might want to default to 02/01/2003 which is the interpretation closest to the current date. Exactly which approach is needed depends entirely on having a clear and full set of rules to generate all the variants. Then we use the same rules to analyse the input. Also - in ambigous cases the default behaviour needs clearer definition.

Edit
You need to think of all the possible things that can occur: what if the file name appears to contain two or more dates?

Edited by czardas
Link to comment
Share on other sites

yea it would be too hard to consider other country formats - the only format i am looking to end up with is MM/DD/YYYY HH:MM TT (if HH:MM TT are not available then 12:00 AM).

in the case of 03/01/02 - yes id prob ignore that because it would be hard to assume thats an actual date (2 digit year is too ambiguous).  

hopefully this makes the playing field easier

thanks again for your help =)

 

Link to comment
Share on other sites

No promises, but I'll see if I can make head or tail of it, after my work. :)

Please explain what you mean by the 'same sequence' in your earlier post: The non-matching file names appear in the same array as the ones which match. I'm not sure how, or why, the examples above or below are intended to influence the interpretaion.

Edited by czardas
Link to comment
Share on other sites

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