Set time to midnight
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I am trying to set default time to 12am (i.e. midnight in client script). The field is type Date/Time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
@Taaha M ,
Why dont you try default value field on the table, why choosing client script?
regards,
Nikhil Bajaj
Regards,
Nikhil Bajaj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
This is in Catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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
Regards,
Nikhil Bajaj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader