This is a very quick note with the code to determine the English ordinal of a date (eg. "st" of "1st"). So in a date, instead of "Tuesday, 6 November 2018", I could want "Tuesday 6th of November 2018".
Why?
Well there's a long a way to do it (but reliable). But being limited to the lines of code you can run (ref. "Maximum number of executable statements", anything that reduces the number of lines used would be an improvement. This is how I was doing it previously.
// get the date (eg. 1, 2, 3, ... 29, 30, 31) This_Day_Date = My_Date_Field.toString("d"); // determine English ordinal English_Ordinal = "th "; if(This_Day_Date == "1" || This_Day_Date == "21" || This_Day_Date == "31") { English_Ordinal = "st "; } if(This_Day_Date == "2" || This_Day_Date == "22") { English_Ordinal = "nd "; } if(This_Day_Date == "3" || This_Day_Date == "23") { English_Ordinal = "rd "; } // display info My_Date.toString("d") + English_Ordinal;
- // get the date (eg. 1, 2, 3, ... 29, 30, 31)
- This_Day_Date = My_Date_Field.toString("d");
- // determine English ordinal
- English_Ordinal = "th ";
- if(This_Day_Date == "1" || This_Day_Date == "21" || This_Day_Date == "31")
- {
- English_Ordinal = "st ";
- }
- if(This_Day_Date == "2" || This_Day_Date == "22")
- {
- English_Ordinal = "nd ";
- }
- if(This_Day_Date == "3" || This_Day_Date == "23")
- {
- English_Ordinal = "rd ";
- }
- // display
- info My_Date.toString("d") + English_Ordinal;
How?
I'm exploring various methods, so here's a couple using the built-in functions of Zoho deluge.
Method #1:
Using a list/array, I'm going to check the date against the 3 exceptions to this rule (11th, 12th and 13th of each month):
// get the date (eg. 1, 2, 3, ... 29, 30, 31) This_Day_Date = My_Date_Field.toString("d"); // determine English Ordinal Ordinals = "th,st,nd,rd".toList(); English_Ordinal = Ordinals.get(0); EO_Index = This_Day_Date.substring(This_Day_Date.length() - 1).toLong(); if((EO_Index == 1 || EO_Index == 2 || EO_Index == 3) && (This_Day_Date != "11" && This_Day_Date != "12" && This_Day_Date != "13")) { English_Ordinal = Ordinals.get(EO_Index); }
- // get the date (eg. 1, 2, 3, ... 29, 30, 31)
- This_Day_Date = My_Date_Field.toString("d");
- // determine English Ordinal
- Ordinals = "th,st,nd,rd".toList();
- English_Ordinal = Ordinals.get(0);
- EO_Index = This_Day_Date.substring(This_Day_Date.length() - 1).toLong();
- if((EO_Index == 1 || EO_Index == 2 || EO_Index == 3) && (This_Day_Date != "11" && This_Day_Date != "12" && This_Day_Date != "13"))
- {
- English_Ordinal = Ordinals.get(EO_Index);
- }
Method #2:
Using a map, set default to "th", check if the date (string) is in the map, if so use mapped value:
// determine English Ordinal English_Ordinal = "th"; Ordinal_Map = {"1":"st","2":"nd","3":"rd","21":"st","22":"nd","23":"rd","31":"st"}; if(Ordinal_Map.containkey(My_Date_Field.toString("d"))) { English_Ordinal = Ordinal_Map.get(My_Date_Field.toString("d")); }
- // determine English Ordinal
- English_Ordinal = "th";
- Ordinal_Map = {"1":"st","2":"nd","3":"rd","21":"st","22":"nd","23":"rd","31":"st"};
- if(Ordinal_Map.containkey(My_Date_Field.toString("d")))
- {
- English_Ordinal = Ordinal_Map.get(My_Date_Field.toString("d"));
- }
Method #3:
Given the current date and using the last character of the date, we can do the following:
v_EnglishOrdinal = "th "; v_ThisDayDateLastChar = zoho.currentdate.toString("d").right(1).toLong(); v_EnglishOrdinal = if(v_ThisDayDateLastChar == 1 && zoho.currentdate.toString("dd")!=11,"st",v_EnglishOrdinal); v_EnglishOrdinal = if(v_ThisDayDateLastChar == 2 && zoho.currentdate.toString("dd")!=12,"nd",v_EnglishOrdinal); v_EnglishOrdinal = if(v_ThisDayDateLastChar == 3 && zoho.currentdate.toString("dd")!=13,"rd",v_EnglishOrdinal);
- v_EnglishOrdinal = "th ";
- v_ThisDayDateLastChar = zoho.currentdate.toString("d").right(1).toLong();
- v_EnglishOrdinal = if(v_ThisDayDateLastChar == 1 && zoho.currentdate.toString("dd")!=11,"st",v_EnglishOrdinal);
- v_EnglishOrdinal = if(v_ThisDayDateLastChar == 2 && zoho.currentdate.toString("dd")!=12,"nd",v_EnglishOrdinal);
- v_EnglishOrdinal = if(v_ThisDayDateLastChar == 3 && zoho.currentdate.toString("dd")!=13,"rd",v_EnglishOrdinal);
Method #4:
Combining method 2 & 3, we can reduce getting the ordinal to 2 lines plus the output:
v_NextRenewalDate = '2018-11-06'; v_ThisDayDate = v_NextRenewalDate.toString("dd").toLong(); v_ThisDayDateLastChar = v_NextRenewalDate.toString("d").right(1).toLong(); v_EnglishOrdinal = if(v_ThisDayDateLastChar<4 && (v_ThisDayDate < 4 || v_ThisDayDate > 20),"st,nd,rd".toList().get(v_ThisDayDateLastChar-1),"th"); // full formal date v_ContractDateFormatted = v_NextRenewalDate.toString("EEEE, d") + v_EnglishOrdinal + " of " + v_NextRenewalDate.toString("MMMM, yyyy"); info v_ContractDateFormatted; // yields Tuesday, 6th of November, 2018
- v_NextRenewalDate = '2018-11-06';
- v_ThisDayDate = v_NextRenewalDate.toString("dd").toLong();
- v_ThisDayDateLastChar = v_NextRenewalDate.toString("d").right(1).toLong();
- v_EnglishOrdinal = if(v_ThisDayDateLastChar<4 && (v_ThisDayDate < 4 || v_ThisDayDate > 20),"st,nd,rd".toList().get(v_ThisDayDateLastChar-1),"th");
- // full formal date
- v_ContractDateFormatted = v_NextRenewalDate.toString("EEEE, d") + v_EnglishOrdinal + " of " + v_NextRenewalDate.toString("MMMM, yyyy");
- info v_ContractDateFormatted;
- // yields Tuesday, 6th of November, 2018
Method #5:
if a list contains:
v_NextRenewalDate = '2018-11-06'; v_EnglishOrdinal = if("123".contains(v_NextRenewalDate.toString("dd").right(1)) && !"11,12,13".contains(v_NextRenewalDate.toString("dd")),"0,st,nd,rd".get(v_NextRenewalDate.toString("dd").right(1)),"th"); // full formal date v_ContractDateFormatted = v_NextRenewalDate.toString("EEEE, d") + v_EnglishOrdinal + " of " + v_NextRenewalDate.toString("MMMM, yyyy"); info v_ContractDateFormatted; // yields Tuesday, 6th of November, 2018
- v_NextRenewalDate = '2018-11-06';
- v_EnglishOrdinal = if("123".contains(v_NextRenewalDate.toString("dd").right(1)) && !"11,12,13".contains(v_NextRenewalDate.toString("dd")),"0,st,nd,rd".get(v_NextRenewalDate.toString("dd").right(1)),"th");
- // full formal date
- v_ContractDateFormatted = v_NextRenewalDate.toString("EEEE, d") + v_EnglishOrdinal + " of " + v_NextRenewalDate.toString("MMMM, yyyy");
- info v_ContractDateFormatted;
- // yields Tuesday, 6th of November, 2018
Method #6:
The previous methods are inconsistent as it depends on the date format of the system. Instead (update 2021), I now use the following:
v_GivenDate = '2021-09-21'; m_Ordinals = {1:"st",21:"st",31:"st",2:"nd",22:"nd",3:"rd",23:"rd"}; v_Ordinal = ifnull(m_Ordinals.get(v_GivenDate.toString("d").toLong()),"th"); info v_Ordinal; // yields st
- v_GivenDate = '2021-09-21';
- m_Ordinals = {1:"st",21:"st",31:"st",2:"nd",22:"nd",3:"rd",23:"rd"};
- v_Ordinal = ifnull(m_Ordinals.get(v_GivenDate.toString("d").toLong()),"th");
- info v_Ordinal;
- // yields st