Convert Date Format (ISO 8601 <=> DIN 1355)

Updated:
All Buttons are now also working with Formats YYYYMDD, YYYYMMD, YYYYMD, DMMYYY, DDMYYY and DMYYYY if they contain separators.
Short Date Formats are completed by adding the missing digits for the century now.

This is a project for me to learn more about Regular Expressions and the use of VB-Script in DOpus.
The Buttons uses leo's Multi-RegExp Script.
I added some parts out of the Titlecase Script from Steve (e.g. "delete multiple spaces").

The Button comes as a Menubutton containing 4 Buttons:

  • Change appearance of date in ISO 8601 format to "YYYY-MM-DD" and move it to the beginning of the filename
  • Change appearance of date in DIN 1355 format to "DD.MM.YYYY" and move it to the end of the filename
  • Convert DIN 1355 to ISO 8601 (YYYY-MM-DD at the beginning of the filename)
  • Convert ISO 8601 to DIN 1355 (DD.MM.YYYY at the end of the filename)
    200dpi_wc10307_dopus

For short formats with 6 digits years between 1900 and 2099 are recognized and the missing digits for the century are added.
Years within the range of "00-19" are always assumed to be 21th century dates.
Years within the range of "20-99" are assumed tobe 20th century dates.
So dates "1900-1919" or "2020-2099" will be transfered to another century.

Long formats are recognized for years between 1900 and 2019.
I set this limit because I think it's not necessary to include dates of the far future and every limitation helps reducing the risk of false-positive matches.

Attention!
You shoud select the files you want to process with these Buttons very carefully!
You should be sure which Date format the filenames have (particularly necessary if there are dates in short formats!) and look if there are any other numbers which could be falsely interpreted as a date by the Script.
Especially sequences of 2 digits followed by spaces or other separators and 6- or 8-digit-blocks run into danger to be messed up by the Sript.

All recognized Formats can contain separators (must be identical between elements of date): [- _,;.]

Recognized Format are:

  • DDMMYY and YYMMDD (unsecure formats, nearly identical)
  • YYYYMMDD and DDMMYYYY
  • YYYYMDD, YYYYMMD and YYYYMD (with separators only)
  • DDMYYY, DMMYYY and DMYYYY (with separators only)

Here's the main Part of the Script with the Regexp's (Example is "Convert DIN 1355 to ISO 8601":

       ' Apply our regular expressions to the filename.

       ' Date Format: ddmmyy for 21th century
       re.Pattern = "(.*)[- _,;.\D](0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])([01][0-9])(\D*)[- _,;.\D](.*)"
       strNameOnly = re.Replace(strNameOnly, "20$5-$4-$3 $1$2 $6$7 ")

       ' Date Format: ddmmyy for 20th century
       re.Pattern = "(.*)[- _,;.\D](0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])([2-9][0-9])(\D*)[- _,;.\D](.*)"
       strNameOnly = re.Replace(strNameOnly, "19$5-$4-$3 $1$2 $6$7 ")

       ' Date Format: dd.mm.yy for 21th century
       re.Pattern = "(.*)[- _,;.\D](0[1-9]|[12][0-9]|3[01])([- _,;.])(0[1-9]|1[012])\3([01][0-9])[- _,;.\D](.*)"
       strNameOnly = re.Replace(strNameOnly, "20$5-$4-$2 $1 $6")
       
       ' Date Format: dd.mm.yy for 20th century
       re.Pattern = "(.*)[- _,;.\D](0[1-9]|[12][0-9]|3[01])([- _,;.])(0[1-9]|1[012])\3([2-9][0-9])[- _,;.\D](.*)"
       strNameOnly = re.Replace(strNameOnly, "19$5-$4-$2 $1 $6")

       ' Date Format: ddmmyyyy
       re.Pattern = "(.*)[- _,;.\D](0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])(19[0-9][0-9]|20[0-1][0-9])[- _,;.\D](.*)"
       strNameOnly = re.Replace(strNameOnly, "$4-$3-$2 $1 $5 ")

       ' Date Format: [0]d.[0]m.yyyy with various Separators
       re.Pattern = "(.*)0?([1-9])([- ._,;])0?([1-9])\3(19[0-9][0-9]|20[0-1][0-9])(.*)"
       strNameOnly = re.Replace(strNameOnly, "$5-0$4-0$2 $1 $6") 

       ' Date Format: dd.[0]m.yyyy with various Separators
       re.Pattern = "(.*)([12][0-9]|3[01])([- ._,;])0?([1-9])\3(19[0-9][0-9]|20[0-1][0-9])(.*)"
       strNameOnly = re.Replace(strNameOnly, "$5-0$4-$2 $1 $6") 

       ' Date Format: [0]d.mm.yyyy with various Separators
       re.Pattern = "(.*)0?([1-9])([- ._,;])(1[012])\3(19[0-9][0-9]|20[0-1][0-9])(.*)"
       strNameOnly = re.Replace(strNameOnly, "$5-$4-0$2 $1 $6") 

       ' Date Format: dd.mm.yyyy with various Separators
       re.Pattern = "(.*)([12][0-9]|3[01])([- ._,;])(1[012])\3(19[0-9][0-9]|20[0-1][0-9])(.*)"
       strNameOnly = re.Replace(strNameOnly, "$5-$4-0$2 $1 $6") 

If you want to change the order of Y-M-D or apply different separators you can edit the lines with "$"-characters. Changing "$5-$4-$2 $1 $6" to "$1 $6 $5-$4-$2" would move the date to the end of the filename.

In VB-Script "$" has the same function as the "[b][/b]" in the Replace section (TO=)of a DOpus Rename command.
Be sure to not destroy the Regexp's. Changing a single character by mistake will mess up your filenames!

The following Options from Steve's "TItlecase"-Preset can be set to "False" if you don't want them to be executed:

       fStripSpaces = True     ' Strip leading/trailing spaces
       fMultiSpace = True      ' Remove multiple spaces
       fOddChars = True        ' Replace %20 with spaces
       fExtLCase = True        ' Make extension lower case
       fUnderscore = True      ' Replace "_" with a space

The " fOddChars" Option should always be "True" because "%20" can be mixed up with the date which would create false dates.
The "fMultiSpace" Option should also be "True" because moving the Date in the filename can result in multiple Spaces.

  • I tried to make it work even with formats like "YYYMDD" or "YYYYMD" but couldn't get it working. Simply adding a "?" to the zeros for Date and Month works at some positions but deletes the Zeros from other filenames when placed at some other positions. Done!
  • The Script could discover if the date contains single digits and add a zero before it (YYYYMD to YYYYMMDD) Done!
  • Converting short formats to long formats would be a nice feature (YYMMDD to YYYYMMDD). Should be possible provided that years between e.g. "00" and "19" are always interpreted to be "2000" to "2019". Done!

I solved this problems by writing a single Regexp-String for each format variation.
All leading Zeros are skipped by the Regexp so I could restore or add them in the Replace-Line.
I applied this only to dates containing separators because it's not possible to make out if a 6-digit sequence is meant as "YYYYMD" or "YYMMDD".
Writing dates in a 7-digits sequence without separators is a mess anyway I think.
Single Regexp's for 20th and 21th century also helped adding the missing century-digits.

I think it's finished now because it's working very good for me.
If you dont't think so or have any trouble with these Buttons, please tell me.

Download:

Convert Date Format (DIN 1355 <=> ISO 9601) Menubutton

This is a toolbar menu-button, not a Rename preset.

To add the menu-button from the .dcf file to your toolbar:
(From: How to use buttons and scripts from this forum)

  • Navigate to where the .dcf file is saved.
  • Select Settings => Customize Toolbars.
  • Drag the .dcf file to your toolbar.
  • Click OK in the Customize window.

I edited and updated my previous Post because it contained some stupid things in the code.

If you downloaded the Menu Button before reading this Post, please download and use the new version.
It's hopefully much better now.

Updated first post again: Now the missing digits for the century are added to dates in short formats.

Once again a small Update:

  • The "Make extension lowercase"- Option didn't work.

  • Cleaned up the VBScript from unnecessary variables.

Hello,

Wondering if it is possible to get this script to recognize dates that are spelt out (March 13 2010) and convert to ISO8601 without the dashes in the middle (2010 03 13)