- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
Hi team
I have this code to fetch and display the date and time in CST but it is displaying in 24 hour format but how can I display it in hh:mm am/pm format?
var time = new GlideDateTime();
var targetTimezone = 'CST';
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
template.print(time + " CST");
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
var time = new GlideDateTime();
// Use proper timezone ID for Central Time (handles CST/CDT automatically)
var targetTimezone = 'CST';
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz); // sets the timezone properly
// Get display value (yyyy-MM-dd HH:mm:ss)
var displayTime = time.getDisplayValue(); // e.g., "2025-09-18 07:08:45"
// Extract date and time parts
var datePart = displayTime.substring(0, 10); // yyyy-MM-dd
var hour24 = parseInt(displayTime.substring(11, 13)); // HH
var minute = displayTime.substring(14, 16); // mm
// Convert to 12-hour format with AM/PM
var period = "AM";
var hour12 = hour24;
if (hour24 == 0) { // Midnight
hour12 = 12;
period = "AM";
} else if (hour24 == 12) { // Noon
hour12 = 12;
period = "PM";
} else if (hour24 > 12) { // Afternoon
hour12 = hour24 - 12;
period = "PM";
} else { // Morning
hour12 = hour24;
period = "AM";
}
// Build final time string
var finalTime = hour12 + ":" + minute + " " + period;
template.print(datePart + " " + finalTime + " CST");
this helped me to resolve my issue.
Thanks everyone for your replies and time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
var time = new GlideDateTime();
// Use proper timezone ID for Central Time (handles CST/CDT automatically)
var targetTimezone = 'CST';
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz); // sets the timezone properly
// Get display value (yyyy-MM-dd HH:mm:ss)
var displayTime = time.getDisplayValue(); // e.g., "2025-09-18 07:08:45"
// Extract date and time parts
var datePart = displayTime.substring(0, 10); // yyyy-MM-dd
var hour24 = parseInt(displayTime.substring(11, 13)); // HH
var minute = displayTime.substring(14, 16); // mm
// Convert to 12-hour format with AM/PM
var period = "AM";
var hour12 = hour24;
if (hour24 == 0) { // Midnight
hour12 = 12;
period = "AM";
} else if (hour24 == 12) { // Noon
hour12 = 12;
period = "PM";
} else if (hour24 > 12) { // Afternoon
hour12 = hour24 - 12;
period = "PM";
} else { // Morning
hour12 = hour24;
period = "AM";
}
// Build final time string
var finalTime = hour12 + ":" + minute + " " + period;
template.print(datePart + " " + finalTime + " CST");
this helped me to resolve my issue.
Thanks everyone for your replies and time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
this link has approach
How to display Date and Time in 12-hour format in Business Rule
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader