Make date/time value in format to MM/dd/yyyy hh:mm:ss and add 14 days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-10-2023 01:47 AM
Hi All,
I have a requirement to create a scheduled job that will update all tasks without due date on cert_follow_task table and make it to 14days from the date of run of the job, but i have stumbled into an issue where the format is returning as yyyy-MM-dd hh:mm:ss i need it to be on format MM/dd/yyyy hh:mm:ss and use the addDays to add 14 days and update the due date, i have been trying to run it but unsuccessful, can anyone help me please??
var task = new GlideRecord('cert_follow_on_task');
task.addEncodedQuery('due_dateISEMPTY^active=true^stateNOT IN3,4,7^state=2^assigned_to=NULL');
task.query();
if (task.hasNext()) {
while (task.next()) {
var nd = new GlideDate();
var date = nd.getByFormat('MM/dd/yyyy hh:mm:ss');
var fd = new GlideDate();
var date2 = fd.getByFormat('MM/dd/yyyy hh:mm:ss');
date2.addDays(14);
task.due_date = date2;
task.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-12-2023 09:46 AM
Actually, to change the default date format, use:
var task = new GlideRecord('cert_follow_on_task');
//task.addEncodedQuery('due_dateISEMPTY^active=true^stateNOT IN3,4,7^state=2^assigned_to=NULL');
task.query();
while (task.next()) {
var date = new GlideDate();
date.addDays(14);
var newDate = date.getByFormat('MM/dd/yyyy');
gs.info("New date = " + newDate + ".");
task.due_date = newDate;
// task.update();
gs.info("New due_date = " + task.due_date + ".");
}
the time format in your example doesn't need changing. However, due_date is a glide DateTime value, and in my instance, the formatting is determined by the Date format set in the 'Basic Configuration' module. So the script has no effect on the formatting when viewing the due_date, but does add 14 days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-12-2023 09:07 AM
Hi @Suz Roque ,
Thanks for bringing into notice, The getDateFormatted is not a function it should be getDateTimeFormat().
To get the 'MM/dd/yyyy hh:mm:ss' format please refer to this post
https://www.servicenow.com/community/in-other-news/the-secrets-of-glidedatetime/ba-p/2290801
Shravan
Please mark this as helpful and correct answer, if this helps you