Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to customise only Time format along with Date in scripting( client /Server) ?

Virendra K
Kilo Sage

Hi All,

 

I am collecting Dates input in client script and passing it to script include.

Client script:

        var startDt = g_form.getValue('start_date');
        var endDt = g_form.getValue('end_date');
        var ga = new GlideAjax('checkDates');   // SI name
        ga.addParam('sysparm_name', 'getallValues');
        ga.addParam('sysparm_startDt', startDt);
        ga.addParam('sysparm_endDt', endDt);
        ga.getXML(callback);
    
 

SI :
var plannedStart = new GlideDateTime(this.getParameter("sysparm_startDt")); 
var plannedEnd = new GlideDateTime(this.getParameter("sysparm_endDt")); 

 

Take an example, I am getting dates like,

'2024-09-28 11:15:01'  

'2024-09-29 15:10:00'

 

But I want to set /customise only Time format of above fetched date like,

'2024-09-28 00:00:01'  

'2024-09-29 11:59:59'

 

How I could do it either in client or server side script ?

 

Thank you.

 

Regards,

VIrendra

 

 

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Virendra K I didn't get the exact requirement but here are two solutions.

 

If you just want to update the time part of a date to a different time.

// Assume we have a GlideDateTime object
var gdt = new GlideDateTime("2024-09-30 10:00:00");

// Let's change only the time to 16:45:00, keeping the same date
gdt.setDisplayValue(gdt.getDisplayValue().split(" ")[0] + " 16:45:00");

gs.info("Modified Date/Time: " + gdt.getDisplayValue());

 OR

If you want to update the date object by adding time in it.

 

// Create a new GlideDateTime object
var gdt = new GlideDateTime();

// Add 2 hours to the current time
gdt.addSeconds(2 * 60 * 60);

gs.info("Updated Date/Time after adding 2 hours: " + gdt.getDisplayValue());

Hope this helps.

View solution in original post

5 REPLIES 5

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @Virendra K

 

To change only the time format for a given date, you can refer the below code:

var startDate = new GlideDateTime("2024-09-29 10:00:00");
var endDate = new GlideDateTime("2024-09-30 11:00:00");

startDate.setValue(startDate.getDisplayValue().split(" ")[0] + " 00:00:01");
gs.log(startDate);

endDate.setValue(endDate.getDisplayValue().split(" ")[0] + " 11:59:59");
gs.log(endDate);

 

Thank You

Juhi Poddar