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

Hello @Virendra K 

 

You can try below script:

 

var todayAtNoon = setTime(new GlideDateTime(), '12:00:00');

 

function setTime(oldDate, time) {
var newDate = new GlideDateTime( '1970-01-01 ' + time);

if (!newDate. isValid()) {
throw new Error( 'Invalid time provided: ' + time);

}

newDate.setYearUTC(oldDate.getYearUTC());
newDate.setMonthUTC(oldDate.getMonthUTC());
newDate.setDayOfMonthUTC(oldDate.getDayOfMonthUTC());
return newDate;

}

 

I hope this answer your query.

 

Thanks & Regards

Juhi Poddar

 

 

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.

Amit Verma
Kilo Patron
Kilo Patron

Hi @Virendra K 

 

Can you please explain the logic behind this conversion ? I am not able to understand how 2024-09-28 11:15:01 becomes 2024-09-28 00:00:01 .

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma 

 

I am checking range of the dates. There are dates stored with timings, so if the input date is '2024-09-30 11:15:01' and actual stored records date is '2024-09-30 08:00:00'  then its getting skipped while fetching it.

 

I want to set any input date/time like '2024-09-30 00:00:01' to '2024-10-01 11:59:59' so that whatever schedules are in between from '2024-09-30 00:00:01'  to '2024-10-01 11:59:59' should be get covered while fetching the records.

 

Thanks,

Virendra