- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2015 06:39 AM
I have a field on a custom form that is a type of Time. We are pulling this value in from a SQL database and we need to find a way to add a value from another field that is an integer field pulled in from the same SQL database that represents how many minutes to pad the time field.
So for instance, the time is 1:00pm - the value of this field comes in to Service Now as 1899-12-30 19:00:00 (the SQL database field is 24 hour format and then GMT ??). All I know is it displays correctly as 1:00pm in Service Now. The preblock field in the SQL database is minutes and we need to be able to subtract these minutes from the Time field and populate that into a new Time field that we can display on a calendar.
So for instance - the SQL value from the time field coming in is 1899-12-30 19:00:00 and the problock value in the SQL database is 15 (minutes). We need a way to calculate that the new time field needs to show 12:45pm (or adjust the value of the original field to be 1899-12-30 18:45:00 to display as 12:45pm).
I could maybe do this by writing a ton of code but I'm not even sure if it would be completely correct. Is anybody great with time calculations and help me out?
Thanks,
Rhonda
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2015 11:17 AM
add this line just after the replace statement:
currTime = new Date(currTime);
I just tested it, it seems to be working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2015 01:58 PM
The problem lies in the format of the string that you are passing to the date object, for some reason is not working in ServiceNow.
You'll need to pass the string in another valid format. Replace the "-" character with "/" and it will work.
Add this line after the currentTime declaration:
currTime = currTime.replace(/-/g, "/");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2015 10:28 AM
Adding that line still gives me invalid results:
var preblock = 15;
var gr = new GlideRecord('u_citav_task');
gr.addQuery('number', 'AVTASK0000129');
gr.query();
if(gr.next()){
var currTime = gr.u_event_start_time;
currTime = currTime.replace(/-/g, "/");
var minutes = preblock;
var millisecondsPerMinute = 60000;
var newTime = new Date(currTime - minutes * millisecondsPerMinute);
gr.u_event_start_time = newTime;
gs.log('currTime var is ' + currTime);
gs.log('minutes var is ' + preblock);
gs.log('newTime is ' + newTime);
}
*** Script: currTime var is 1899/12/30 19:00:00
*** Script: minutes var is 15
*** Script: newTime is Invalid Date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2015 11:17 AM
add this line just after the replace statement:
currTime = new Date(currTime);
I just tested it, it seems to be working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2015 12:44 PM
Great - thanks. That did work!