The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

5 REPLIES 5

mujeebqasimi
Mega Contributor

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');

 

https://www.servicenow.com/community/developer-forum/what-is-the-function-of-glidedatetime-api-settz...

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.

Bhuvan
Kilo Patron

@RahulRAJAS 

 

Refer below article for 12 hour or 24 hour format

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0829705

 

https://www.servicenow.com/community/developer-forum/how-to-display-date-and-time-in-12-hour-format-...

 

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

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.