12 Hour Time: Simple Solution
EDIT 07-FEB-2012: I made a free marketplace app for you all to test
There are a number of posts here about TIME in ZOHO Creator. Many have requested ZOHO supporting a 12 hour format. With some help from Gave a while back, I have been using a rather long function to verify if a user typed in proper 12 hour format (i.e. 12:15 am or 05:00 pm but NOT 1:15 or 07:00 PM). I found a better way to do this that only uses a few lines of code.
This uses ZOHO Creator's support for regular expressions (see a library of such things here). The code is below:
- test = (input.TIME).replaceFirst("^(1[0-2]|0[1-9]):[0-5]\d [ap]m","");
- if (test == "")
- {
- alert("correct");
- }
- else
- {
- alert("wrong format");
- }
I'm no expert at RegEx, but I think the expression is at least mostly correct. It allows users typing extra spaces after the am or pm, though. I fixed that by allowing the text box only 8 characters.
Once you have validated the format, a little more ZOHO code can combine that with a date to obtain a date-time value. You'll need to convert this into 24 hr format (the second if block).
- test = (input.TIME).replaceFirst("^(1[0-2]|0[1-9]):[0-5]\d [ap]m","");
- if (test == "")
- {
- DT_String = zoho.currentdate + " " + (input.TIME).replaceAll(" [ap]m","") + ":00";
- Date_Time = DT_String.toTime();
- if (input.TIME.contains("pm"))
- {
- Date_Time = Date_Time.addHour(12);
- }
- alert(Date_Time);
- }
- else
- {
- alert("wrong format");
- }