Jump to content

Complex Time Formatting Rule

Recommended Posts

Looking for some help on formatting time. If this is even possible. There will be several Time fields on the item so I would also like to know if I will need this rule for each field or can it be called somehow for multiple fields.


There will be two drop downs to pick a start and end time. The final output needs to be formatted in a particular way. Here are examples of the possible inputs from the drop downs and how the final time needs to be formatted.


Drop Down 1-------Drop Down 2----------Final Output Should Be

10:00 a.m.-------------11:00 a.m.-------------10 – 11 a.m.

11:15 a.m.-------------Noon-------------------11:15 a.m. – Noon

11:30 a.m.-------------12:30 p.m.-------------11:30 a.m. – 12:30 p.m.

2:00 p.m.---------------3:15 p.m.--------------2 – 3:15 p.m.

8:00 a.m.---------------------------------------8 a.m.

2:15 p.m.---------------------------------------2:15 p.m.


Any help would be appreciated.

Thank you.

Edited by draco_66
Link to comment
Share on other sites

This should work for you. Just a precaution though, you need to make sure you put that space in between the time and the meridiems.


This works by splitting the time up into 3 sections, the hour, the minutes, and the meridiems.

This first check is to see if one of the 2 drop downs is not used and just outputs what is there.

The second checks if the meridiems match, if so it just uses the hour of the first drop down

Lastly, if both drop downs are used and the meridiems dont match, it outputs the values as is with a hyphen between them.


I have no doubt there is a more succinct way to achieve this, but this should work for what it is.


var dd1_full = Field("Drop Down 1");
var dd2_full = Field("Drop Down 2");

var dd1_split = dd1_full.split(/[\:\s]/);
var dd2_split = dd2_full.split(/[\:\s]/);

if (!dd1_full || !dd2_full)
return dd1_full + dd2_full;
else if (dd1_split[2] == dd2_split[2])
return dd1_split[0] + " - " + dd2_full;
return dd1_full + " - " + dd2_full;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...