- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-30-2020 04:17 PM
I am making a scheduled job to automate our Sprint creation. I am trying to populate a Start Date and an End Date (date-type fields without time). I have found that getByFormat('MM-dd-yyyy') using new GlideDate() allows me to insert the date. However, I am doing some calculations that involve the day of the week (I always want the sprint to start on a Monday) and I'm not sure how to do this with GlideDate().
The other option I considered is using Date() to somehow get the format to be 'MM-dd-yyyy'. Javascript allows toLocaleDateString() with new Date() on W3Schools but not within ServiceNow. Receiving the following when using toLocaleDateString within ServiceNow:
February 3, 2020
W3Schools: 1/3/2020
The Date type field does not accept this.
// Find today's day of the week - startDay (Mon - Sun)
var startDate = new Date();
startDate.setHours(0,0,0,0);
var startDay = startDate.getDay();
// Set startDate to the very next Monday if not already Monday (1)
if(startDay != 1){
// 8 = Monday
// addDays is days to get to Monday
var addDays = 8 - startDay;
startDate.setDate(startDate.getDate() + addDays);
}
// Set end day to Friday - TESTED
var endDate = new Date();
endDate.setHours(0,0,0,0);
endDate.setDate(startDate.getDate() + 11);
// Set start_date and end_date on new Sprint record
grSprint = new GlideRecord('x_bari2_barings_st_sprint');
grSprint.initialize();
grSprint.start_date = startDate;
grSprint.end_date = endDate;
grSprint.state = 'new';
grSprint.update();
gs.info(grSprint.number + ' has been created.');
gs.info(startDate);
gs.info(startDate.getByFormat("MM-dd-yyyy"));
At this point, startDate = Mon Feb 03 2020 00:00:00 GMT-0800 (PST)
The table date fields accept strings in MM-dd-yyyy ('01/14/2020'). I am in a scoped application, running this background script from the scoped application.
Any help would be appreciated.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-30-2020 05:24 PM
This seems to do what you are looking to do. You can run this in scripts - background: Note, I included a line to set the date manually for testing...
//Create a new date object with the current date.
var startDate = new GlideDate();
gs.info('system start date is: ' + startDate);
startDate.setValue('2020-01-30');
gs.info('Your selected date is: ' + startDate);
var startDay = startDate.getDayOfWeekUTC();
gs.info(startDay);
//Now we set the start date to the next Monday
if(startDay != 1){
var addDays = 8 - startDay;
startDate.addDays(addDays);
}
//Now we set the end date to Friday in two weeks
var endDate = new GlideDate();
endDate.setValue(startDate)
endDate.addDays(11);
gs.info('Final Start Date is: ' + startDate)
gs.info('Final End Date is: ' + endDate);
//Here we get the format you want
var formatedStartDate = startDate.getByFormat('dd-MM-yyyy');
var formatedEndDate = endDate.getByFormat('dd-MM-yyyy');
gs.info('Formatted start date is: ' + formatedStartDate);
gs.info('Formatted start date is: ' + formatedEndDate);
That seems to set the dates based on the description of what you want. Hopefully that helps!
If this was helpful or correct, please be kind and remember to click appropriately!
Michael Jones - Proud member of the Cloudpires team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-30-2020 05:24 PM
This seems to do what you are looking to do. You can run this in scripts - background: Note, I included a line to set the date manually for testing...
//Create a new date object with the current date.
var startDate = new GlideDate();
gs.info('system start date is: ' + startDate);
startDate.setValue('2020-01-30');
gs.info('Your selected date is: ' + startDate);
var startDay = startDate.getDayOfWeekUTC();
gs.info(startDay);
//Now we set the start date to the next Monday
if(startDay != 1){
var addDays = 8 - startDay;
startDate.addDays(addDays);
}
//Now we set the end date to Friday in two weeks
var endDate = new GlideDate();
endDate.setValue(startDate)
endDate.addDays(11);
gs.info('Final Start Date is: ' + startDate)
gs.info('Final End Date is: ' + endDate);
//Here we get the format you want
var formatedStartDate = startDate.getByFormat('dd-MM-yyyy');
var formatedEndDate = endDate.getByFormat('dd-MM-yyyy');
gs.info('Formatted start date is: ' + formatedStartDate);
gs.info('Formatted start date is: ' + formatedEndDate);
That seems to set the dates based on the description of what you want. Hopefully that helps!
If this was helpful or correct, please be kind and remember to click appropriately!
Michael Jones - Proud member of the Cloudpires team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2020 12:37 PM
Thank you so much for the help! I struggled with this one for a while and looked over so many previous data formatting posts.
Because I was in an Application Scope I did have to tack on UTC at the end of addDays():
startDate.addDaysUTC(x);
Thanks again!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-30-2020 07:19 PM
Hello Gage,
Refer this link, hope this will help you:
If you find link is useful to you, mark it as Correct and Helpful.
Warm Regards,
Prithviraj Howal
SNOW Developer