The CreatorCon Call for Content is officially open! Get started here.

How do I round a DateTime field to Days?

neil_b
Tera Guru

I have a date field being calculated right now on a scheduled trigger, from the date than an approval was sent out, and it's doing a dateDiff on the current day to return the number of days an approval has been open for.

var approvalRecord = new GlideRecord('sysapproval_approver');
while (approvalRecord.next()) {
  var dat = new GlideDate();
  var datedif = gs.dateDiff(approvalRecord.sys_created_on,dat);
  approvalRecord.u_days_open = datedif;
  approvalRecord.update();

 The end result is that my Days Open field returns DD : TTTT (i.e. 2 08:30:00) so it was sent out 2 days and 8.5 hours ago.

 

What I need it to do, is convert '2 08:30:00' so that it rounds the days, so if it's anything less than 12 hours it rounds down which in this case it would only display '2' OR if it is anything greater than 12 hours (let's say 2 19:45:00), it rounds up so it would only display '3'. We essentially would only like to know the number of days, and not the number or days + hours.

 

How could I update my code to round the days up or down and only return an integer?

1 ACCEPTED SOLUTION

Vijay Balotia1
Tera Guru

Hi @neil_b ,

if you are getting your current output as (i.e. 2 08:30:00)  then to convert the remaining hours in rounds of your can try this.

var approvalRecord = new GlideRecord('sysapproval_approver');
while (approvalRecord.next()) {
  var dat = new GlideDate();
  var datedif = gs.dateDiff(approvalRecord.sys_created_on,dat);
  var splitValue=datedif.toString().split(" ");
  var days=splitValue[0].split(":");
  var time = splitValue[1].split(":");
  var hour = time[0];
    if(hour > 12){
      days++
    }
  approvalRecord.u_days_open = days;
  approvalRecord.update();

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks
Vijay Balotia

View solution in original post

1 REPLY 1

Vijay Balotia1
Tera Guru

Hi @neil_b ,

if you are getting your current output as (i.e. 2 08:30:00)  then to convert the remaining hours in rounds of your can try this.

var approvalRecord = new GlideRecord('sysapproval_approver');
while (approvalRecord.next()) {
  var dat = new GlideDate();
  var datedif = gs.dateDiff(approvalRecord.sys_created_on,dat);
  var splitValue=datedif.toString().split(" ");
  var days=splitValue[0].split(":");
  var time = splitValue[1].split(":");
  var hour = time[0];
    if(hour > 12){
      days++
    }
  approvalRecord.u_days_open = days;
  approvalRecord.update();

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks
Vijay Balotia