- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 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
5 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
5 hours ago
Hi Rahul,
I don't know your use case. But you don’t need to add the offset manually, setTZ() already handles that part. Just use getByFormat() to control the display format. Also, instead of "CST" (which doesn’t account for daylight savings), use "America/Chicago" for Central Time. Example:
var gdt = new GlideDateTime();
gdt.setTZ('America/Chicago');
template.print(gdt.getByFormat('hh:mm a') + ' CT');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi Mujeeb
thanks for your time & reply.
I have tried this
gdt.setTZ('America/Chicago');
it didn't work that's why I have used CST.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago - last edited 5 hours ago
Refer below article for 12 hour or 24 hour format
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0829705
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 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.