How to round off seconds and minutes to hours for a GlideDatetime field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 02:50 AM
Hi,
We have a requirement to round off the seconds and minutes of a datetime field into hours.
For example, if we have a date as "10-13-2017 08:54:59", it should be round off to hours "10-13-2017 09:00:00" .
If it is not possible, atleast it should set the minutes and seconds to zero (10-13-2017 08:00:00);
I have tried the following way to do this, but it is not completely helping out
var testDate = new GlideDateTime(current.start_date);
var time = testDate.getTime().getDisplayValue();
var date = testDate.getDate().getDisplayValue();
var splittime = time.split(":");
var hours = splittime[0];
var minutes = splittime[1];
var seconds = splittime[2];
gs.log("hours:::"+hours+"::minutes:::"+minutes+"::seconds"+seconds);
var testDateAfter = date+" "+hours+":00"+":00";
current.start_date.setDisplayValue(testDateAfter);
The updated value (testDateAfter) was showing and has been with 1 hour less i.e.. (10-13-2017 07:00:00) in the date field
Could you please quickly help us if any simple/correct way to acheive this round off ASAP?
Quick reply will be much appreciated! Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 03:55 AM
Hi Pavan,
Can you try below method in background script:
var testDate = new GlideDateTime('10-13-2017 08:54:59');
var date = testDate.toString().split(" ")[0];
var time = testDate.toString().split(" ")[1];
var hours = time.split(":")[0];
var finalDateTime = date + " " + hours + ":00:00";
gs.print('final value is:'+finalDateTime);
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 04:05 AM
Hi Ankur,
Thanks for reply!
How to set the final value to a field? is with .setValue() or setDisplayValue()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 05:04 AM
Hi Pavan,
Try using setValue()
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 10:59 AM
Hi Pavan,
Any update on this?
Whether you were able to achieve your requirement. If yes then please mark my answer as correct, helpful and hit like. This helps in removing this question from unanswered list.
Thanks in advance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader