Changing dates to ISO format: YYYY-MM-DD

A swapping-ground for Regular Expression syntax

Changing dates to ISO format: YYYY-MM-DD

Postby tony7896 » Tue Aug 16, 2022 4:27 pm

I've got a method for changing date formats that works well enough, but am looking for a slight improvement.

I am using BRU to change the date format in hundreds of thousands of old folder names and file names to the ISO standard date format: YYYY-MM-DD. The current date format in my case is YYYY.MM.DD. Searching through existing posts on this board I've found a regular expression that works almost perfectly:

Match: (.*)(\d{4})\.(\d{2})\.(\d{2})(.*)
Replace: \1\2-\3-\4\5

I found only 2 limitations:

1) It changes only 1 date in each name, whoever simply running it twice handles up to 2 dates per name, which worked fine for me.

2) More seriously, there were cases where there were strings within file names that had the format 9999.99.99 that weren't in fact dates.

The 2nd shortcoming would be fixed if the regular expression looked only for the initial year string to start with "20". Can anyone tell me how I might tweak the regular expression above to accomplish this?
tony7896
 
Posts: 2
Joined: Tue Aug 16, 2022 4:07 pm

Re: Changing dates to ISO format: YYYY-MM-DD

Postby therube » Tue Aug 16, 2022 7:22 pm

Match: (.*)(20\d{2})\.(\d{2})\.(\d{2})(.*)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

20yy.mm.dd ===> 20yy-mm-dd

Postby Luuk » Wed Aug 17, 2022 6:32 am

Yes, therube's match does properly conduct dates 99% of the time, so its probably 100% for most users.
If needing to be very exact, can put a checkmark inside for "v2" with a very long "Match" and "Replace" like...

(?<!\d)(?!..(?:0[1-35-79]|1[013-57-9])\.02\.29)(20)(?!2[3-9])([0-2]\d)\.(?!(?:0[469]|11)\.31)(?!02\.3)(?!00)([01]\d)(?<!1[3-9])\.(?!00)([0-3]\d)(?<!3[2-9])(?!\d)/g
$1$2-$3-$4

Please to conduct a thorough preview of the new-names, because its from... https://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=12&t=4781#p12740
Except that Im editing the "_" separator, and moving the year-groups to the front, so then also having to edit the look-arounds!
It should rename just like...

From2012.01.01to2012.01.15-cc ====> From2012-01-01to2012-01-15-cc
2012.01.01 ======================> 2012-01-01
2020.02.29 ======================> 2020-02-29
2021.02.29 ======================> No changes, because 02.29 only visits within leap-years
2023.01.01 ======================> No changes, because of future-year
2012.13.01 ======================> No changes, because of bad mm
2012.01.32 ======================> No changes, because of bad dd
2016.06.31 ======================> No changes, because of bad dd (only 30-days in June)
12012.01.011 ====================> No changes, because of digits touching the date
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Changing dates to ISO format: YYYY-MM-DD

Postby tony7896 » Fri Aug 19, 2022 7:19 am

Thank you therube and Luuk! I'll be looking forward to giving these a try. :D
tony7896
 
Posts: 2
Joined: Tue Aug 16, 2022 4:07 pm


Return to Regular Expressions