A quick article to remind me of the regex to change a date from the format dd/MM/yyyy to yyyy-MM-dd.
Why?
Irrespective of server, organization or user settings, the date handling can vary. If we are getting a date from a CSV or other external source as "dd/MM/yyyy", how do we guarantee that the system will understand the date correctly?
How?
It may be that when obtaining a date string and applying the method .toString("dd/MM/yyyy") is dependent on the settings. But making a date into a SQL format or from largest denominator to smallest in "yyyy-MM-dd" will usually handle the date better. Consider the following:
v_TestDate = "07/01/1975"; info v_TestDate; info v_TestDate.toDate(); info v_TestDate.toString("E, d MMM yyyy"); info "*********************"; // // regex change dd/MM/yyyy to yyyy-MM-dd v_RegexdDate = v_TestDate.replaceAll("(\\d{2})/(\\d{2})/(\\d{4})","$3-$2-$1", false); info v_RegexdDate; info v_RegexdDate.toDate(); info v_RegexdDate.toString("E, d MMM yyyy"); info "*********************"; // // pure Deluge no Regex v_DelugeDate = v_TestDate.toList("/").get(2) +"-"+ v_TestDate.toList("/").get(1) +"-"+ v_TestDate.toList("/").get(0); info v_DelugeDate; info v_DelugeDate.toDate(); info v_DelugeDate.toString("E, d MMM yyyy");
- v_TestDate = "07/01/1975";
- info v_TestDate;
- info v_TestDate.toDate();
- info v_TestDate.toString("E, d MMM yyyy");
- info "*********************";
- //
- // regex change dd/MM/yyyy to yyyy-MM-dd
- v_RegexdDate = v_TestDate.replaceAll("(\\d{2})/(\\d{2})/(\\d{4})","$3-$2-$1", false);
- info v_RegexdDate;
- info v_RegexdDate.toDate();
- info v_RegexdDate.toString("E, d MMM yyyy");
- info "*********************";
- //
- // pure Deluge no Regex
- v_DelugeDate = v_TestDate.toList("/").get(2) +"-"+ v_TestDate.toList("/").get(1) +"-"+ v_TestDate.toList("/").get(0);
- info v_DelugeDate;
- info v_DelugeDate.toDate();
- info v_DelugeDate.toString("E, d MMM yyyy");
07/01/1975 01-Jul-1975 Tue, 1 Jul 1975 ********************* 1975-01-07 07-Jan-1975 Tue, 7 Jan 1975 ********************* 1975-01-07 07-Jan-1975 Tue, 7 Jan 1975
- 07/01/1975
- 01-Jul-1975
- Tue, 1 Jul 1975
- *********************
- 1975-01-07
- 07-Jan-1975
- Tue, 7 Jan 1975
- *********************
- 1975-01-07
- 07-Jan-1975
- Tue, 7 Jan 1975
Additional
You would of course need to check that there is nothing wrong with the date that was submitted. Especially if it is coming from a data source, the Zoho deluge .toList() fails the function if the .get is an out-of-bounds and the .toDate() or .toString() fall apart with an unparsable date:
v_TestDate = "07011975"; info "*********************"; // // regex change dd/MM/yyyy to yyyy-MM-dd v_RegexdDate = v_TestDate.replaceAll("(\\d{2})/(\\d{2})/(\\d{4})","$3-$2-$1", false); if(isDate(v_RegexdDate)) { info v_RegexdDate.toDate(); info v_RegexdDate.toString("E, d MMM yyyy"); } info v_RegexdDate; info "*********************"; // // pure Deluge no Regex v_DelugeDate = null; if(v_TestDate.toList("/").size()>2){ v_DelugeDate = v_TestDate.toList("/").get(2) +"-"+ v_TestDate.toList("/").get(1) +"-"+ v_TestDate.toList("/").get(0); info v_DelugeDate.toDate(); info v_DelugeDate.toString("E, d MMM yyyy"); } info v_DelugeDate;
- v_TestDate = "07011975";
- info "*********************";
- //
- // regex change dd/MM/yyyy to yyyy-MM-dd
- v_RegexdDate = v_TestDate.replaceAll("(\\d{2})/(\\d{2})/(\\d{4})","$3-$2-$1", false);
- if(isDate(v_RegexdDate))
- {
- info v_RegexdDate.toDate();
- info v_RegexdDate.toString("E, d MMM yyyy");
- }
- info v_RegexdDate;
- info "*********************";
- //
- // pure Deluge no Regex
- v_DelugeDate = null;
- if(v_TestDate.toList("/").size()>2){
- v_DelugeDate = v_TestDate.toList("/").get(2) +"-"+ v_TestDate.toList("/").get(1) +"-"+ v_TestDate.toList("/").get(0);
- info v_DelugeDate.toDate();
- info v_DelugeDate.toString("E, d MMM yyyy");
- }
- info v_DelugeDate;
********************* 07011975 ********************* null
- *********************
- 07011975
- *********************
- null
Source(s):