Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

convert the 24 hr time format to 12 hr format

RahulRAJAS
Kilo Guru

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");
1 ACCEPTED SOLUTION

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.

View solution in original post

6 REPLIES 6

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@RahulRAJAS 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader