by RegexNinja » Fri Mar 27, 2020 7:13 pm
I dont know about easier.. I'm still getting used to javascript, lol.. Here's what I first tried:
xxx = name.substr(0,8).split('_',3)
newName = new Date(xxx).format('{year}_{MM}_{dd}') + name.substr(8)
It did verify legitimate dates, but could've potentially inserted 'Invalid Date' into some filenames.
So then I tried creating an 'if not' for newName like this:
xxx=name.substr(0,8).split('_',3)
yyy = new Date(xxx).format('{year}_{MM}_{dd}')
if (yyy!='Invalid Date') {newName=yyy+name.substr(8)}
Worked like a charm.. Not sure if that's the easiest way to handle it though.. Too much of noob right now.
It works just like the regex, except that is doesnt filter-out the future years beyond 2020..
To be honest, Im not sure why I did that.. Its unnecessarily complicated.. Guess I got carried away, lol.
I did mean for the regex-solution to filter-out the 19xx years though, since there's no such thing as conditional replacements.
I mean no such thing as 19 OR 20 in the replacement, that's where javascript wins out on this one, handling all valid dates.
The javascript also fixes bad days: (but not months>12 or days>31)
06_31_16 -----> 2016_07_01 (Jun31->Jul01)
11_31_16 -----> 2016_12_01 (Nov31->Dec01)
02_29_17 -----> 2017_03_01 (NonLeapYear, Feb29->Mar01)
Cheers!