Set time to midnight

Taaha M
Tera Contributor

I am trying to set default time to 12am (i.e. midnight in client script).  The field is type Date/Time.

5 REPLIES 5

Nikhil Bajaj9
Giga Sage

@Taaha M ,

 

Why dont you try default value field on the table, why choosing client script?

 

regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

This is in Catalog item

Hi @Taaha M ,

 

Does it means it is a varibale of Date/time type and you want to set default Time as 12 in the caledar.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

Ankur Bawiskar
Tera Patron
Tera Patron

@Taaha M 

you can use onLoad catalog client script for this.

try this

 

function onLoad() {
    var field = 'your_date_time_field';
    if (!g_form.getValue(field)) {
        var d = new Date();
        d.setHours(0, 0, 0, 0); // midnight local
        g_form.setValue(field, formatDateTime(d));
    }
}

function formatDateTime(date) {
    var p = n => n < 10 ? '0' + n : n;
    return date.getFullYear() + '-' + p(date.getMonth() + 1) + '-' + p(date.getDate()) +
        ' ' + p(date.getHours()) + ':' + p(date.getMinutes()) + ':' + p(date.getSeconds());
}

 

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