Advertisement
Guest User

AR Date reformat

a guest
Sep 26th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ### Stuff you can change, right below
  2. //Possible date separator in original filename. You can put more in there.
  3. const DATE_SEP = '-_';
  4.  
  5. // date separator for the final filename. Can be changed to taste.
  6. const NEW_DATE_SEP = '-';
  7.  
  8. // Possible characters before the date to remove
  9. const BFR_DATE_SEP = ' _';
  10.  
  11. // Filename displayed when the regex expression can't find what it's looking for
  12. const REGEX_ERROR = '-Regex Error-';
  13.  
  14. // Day and month digit size minimum (pad with zero if necessary)
  15. const MNT_DAY_SIZE = 2
  16.  
  17. // ### From here, mostly Stuff you shouldn't change, unless you know what you're doing of course.
  18. // positions of the regex resulting array, to clarify things
  19. const FULL_RSLT = 0, BEF_DATE = 1, DATE_DAY = 2, DATE_MNT = 3, DATE_YR = 4, AFT_DATE = 5;
  20.  
  21. //final values for the date components will be stored here.
  22. var newDay = "", newMonth = "", newYear = "";
  23.  
  24. // Regular expression to split the filename around the date.
  25. var reDateExtract = new RegExp("(.*?)[" + BFR_DATE_SEP + "]?0?(\\d{1,2})[" + DATE_SEP + "]0?(\\d{1,2})[" + DATE_SEP + "](\\d{2})[\\s_]?(.*)", "");
  26.  
  27. if (reDateExtract.test(item.newBasename) == False)
  28.   { return REGEX_ERROR; }
  29.  
  30. // Run the regex and return it in an array
  31. aDate_n_Extra = reDateExtract.exec(item.newBasename);
  32.  
  33. // add proper century to the year
  34. if (aDate_n_Extra[DATE_YR] >= 30 && aDate_n_Extra[DATE_YR] <=99)
  35.     { newYear = '19' + aDate_n_Extra[DATE_YR]; }
  36. else
  37.     { newYear = '20' + aDate_n_Extra[DATE_YR]; }
  38.  
  39. // padding day and month with a zeros
  40. newDay = zpad(aDate_n_Extra[DATE_DAY], MNT_DAY_SIZE);
  41. newMonth = zpad(aDate_n_Extra[DATE_MNT], MNT_DAY_SIZE);
  42.  
  43. // Final filename
  44. return aDate_n_Extra[BEF_DATE] + aDate_n_Extra[AFT_DATE]  + ' ' + [newYear, newMonth, newDay].join(NEW_DATE_SEP);
  45.  
  46. // add leading zeros to any string
  47. function zpad(str, nb_digits)
  48. {
  49.     if (str.length < nb_digits)
  50.         { return (Array(nb_digits - str.length + 1).join('0') + str); }
  51.     else
  52.         { return str; }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement