gethour()

ValeriaS
Tera Contributor

I am trying to create a script to get the month and the hour an incident was closed and enter it in different field. However, the hour is returing in a 12h format instead of 24h. Any Ideas on how to solve this? Here is my script:

(function executeRule(current, previous /*null when async*/) {
    if (current.closed_at) {
        gs.info("closed_at field is present: " + current.closed_at);
        try {
            var gdt = new GlideDateTime(current.closed_at);
            gs.info("GlideDateTime: " + gdt.getDisplayValue());

            var localGdt = gdt.getLocalTime();
            gs.info("Local GlideDateTime: " + localGdt.getDisplayValue());

            // Get the closed hour
            var closedHourLocal = localGdt.getHourLocalTime();
            gs.info("Closed hour (local): " + closedHourLocal);

            // Get the raw hour correctly
            var rawHour = localGdt.getHour();
            gs.info("Raw hour from localGdt: " + rawHour);

            current.u_closed_hour = closedHourLocal;
            current.update();
        } catch (e) {
            gs.info("Error processing closed_at: " + e.message);
        }
    } else {
        gs.info("closed_at field is empty or invalid.");
    }
})(current, previous);
2 REPLIES 2

Animesh Das2
Mega Sage

Hi @ValeriaS ,

 

Can you please simply get the display value of the 'closed_at' and then split and take the 1st element of the array as shown below,

var closedAt = (new GlideDateTime(current.closed_at).getDisplayValue());

var closedTime = closedAt.split('<space>')[1];

 

Please check if it works for you.

 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

 

Animesh Das2
Mega Sage

Hi @ValeriaS ,

 

If you got chance to check the solution and help you anyways, please mark this helpful or if it addresses your query correctly please Accept as Solution so that it helps the community for other users as well.

 

Otherwise share if you are facing any challenges there.

 

Thanks,

Animesh Das