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
‎07-03-2017 10:21 AM
Hi Pavan,
Any update on this?
Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps people to find similar question easily. Thanks in advance.
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 04:26 AM
Hi Pavan,
There is another short script which you can try,
if you have before BR you can have only one line script or if you have after BR you need to use current.update() :
current.closed_at.setValue(current.closed_at.toString().slice(0,-5)+"00:00");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2022 09:07 AM
Here is the client script I used to set seconds to 00 and round the minutes to the nearest quarter hour.
I don't have the math for hour rounding here but it should give a base to start from.
This is best set in an onChange script for the date and time field you want to modify.
// Rounds seconds to 00 and minutes to the nearest quarter hour
var startDateTime = new Date(g_form.getValue("planned_start_date"));
var roundQuarterHour = ("0" + (((startDateTime.getMinutes() + 7.5)/15 | 0) * 15) % 60).slice(-2);
var roundedTime = startDateTime.getFullYear() + "-" + ("0" + (startDateTime.getMonth() + 1)).slice(-2) + "-" + ("0" + startDateTime.getDate()).slice(-2) + " " + ("0" + startDateTime.getHours()).slice(-2) + ":" + roundQuarterHour + ":" + "00";
g_form.setValue("planned_start_date", roundedTime);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-11-2022 01:05 PM
I have updated my script to work much better now.
You can find it here: