- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 10:22 AM
I am writing a Business Rule to build a calculated field by concatenating some fields. I have already modified a date and time field with this code:
var tz = Packages.java.util.TimeZone.getTimeZone("America/Chicago");
var time = new GlideDateTime();
time.setTZ(tz);
time.setValue(current.begin);
time.setValue(current.end);
This was to fix the time zone difference.
However, for end users, I would prefer to display the date and time in a 12-hour format instead of the 24-hour format, which would include AM and PM at the end. I have done a lot of research but haven't found any functionality that could convert the format. I figure I could code it to build the 12-hour format using conditional statements based on what the time value was, but that would be a bit messy. Is there another way?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 10:58 AM
There is no easy way to do this other than taking the string value and writing a small script to replace the hours and add the appropriate AM/PM designator. (There are ways, but they use Java and are restricted in SN).
You can try something like this:
function convertDateTo12HourFormat(isoDateStr) {
return isoDateStr.replace(/([^\s]+\s)(\d{2})(.+)/, function(x, date, h, ms) {
h = parseInt(h, 10);
var ampm = h > 11 ? 'PM' : 'AM';
if (h > 12) h = h - 12;
if (10 > h) h = '0' + h;
return date + h + ms + ' ' + ampm;
});
}
convertDateTo12HourFormat('2015-06-12 18:52:34'); // 2015-06-12 06:52:34 PM
Let me know if it needs explaining.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 10:58 AM
There is no easy way to do this other than taking the string value and writing a small script to replace the hours and add the appropriate AM/PM designator. (There are ways, but they use Java and are restricted in SN).
You can try something like this:
function convertDateTo12HourFormat(isoDateStr) {
return isoDateStr.replace(/([^\s]+\s)(\d{2})(.+)/, function(x, date, h, ms) {
h = parseInt(h, 10);
var ampm = h > 11 ? 'PM' : 'AM';
if (h > 12) h = h - 12;
if (10 > h) h = '0' + h;
return date + h + ms + ' ' + ampm;
});
}
convertDateTo12HourFormat('2015-06-12 18:52:34'); // 2015-06-12 06:52:34 PM
Let me know if it needs explaining.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 11:08 AM
I did see the Java methods online, but didn't think they could be used in SN Business Rules. Your code is similar to what I would have done.
I have a guess, but what is the purpose of your "replace" function?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 11:10 AM
It's replacing the original date string using a regular expression pattern match. The anonymous function then takes the output of the pattern match, does the work and returns what you need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2016 02:04 AM
hi james,
I need to restrict the time like (whenever user provide the input for time, it should be in between 12PM to 5PM.) can you please guide me ?