Return hours from datetime in script

Dan Johnson
Tera Contributor

This should be easy but it isn't. I am trying to create a bucket group for hourly intervals for a breakdown. I need to return the hour of they day from when the ticket was opened.

This does not give me any errors, but I know it's wrong because the milliseconds will include the date as well and not just the hours from the day.

var hours= function(y){return y.dateNumericValue()/(60*60*1000);};

hours(current.opened_at);

The bucket group confirms by throwing this into a really high numbered bucket. So it does return a number.

Any ideas?

1 ACCEPTED SOLUTION

johnnyd54
Giga Expert

I found this somewhere else on the community but I can't find where. It basically   just grabs the hour from the field and assigns it a number (tr) that goes in the corresponding bucket. I had to modify it from the original a little because of the time zones but the original had if cr[0] == '00', tr =0.



var hr = function(x){



  var br = x.split(' ');



  var cr = br[1].toString().split(':');



  var tr = 1;



  if (cr[0] == '00') { tr = 20; gs.log(tr);}



  else if (cr[0] == '01') { tr = 21; gs.log(tr);}



  else if (cr[0] == '02') { tr = 22; gs.log(tr);}



  else if (cr[0] == '03') { tr = 23; gs.log(tr);}



  else if (cr[0] == '04') { tr = 0; gs.log(tr);}



  else if (cr[0] == '05') { tr = 1; gs.log(tr);}



  else if (cr[0] == '06') { tr = 2; gs.log(tr);}



  else if (cr[0] == '07') { tr = 3; gs.log(tr);}



  else if (cr[0] == '08') { tr = 4; gs.log(tr);}



  else if (cr[0] == '09') { tr = 5; gs.log(tr);}



  else if (cr[0] == '10') { tr = 6; gs.log(tr);}



  else if (cr[0] == '11') { tr = 7; gs.log(tr);}



  else if (cr[0] == '12') { tr = 8; gs.log(tr);}



  else if (cr[0] == '13') { tr = 9; gs.log(tr);}



  else if (cr[0] == '14') { tr = 10; gs.log(tr);}



  else if (cr[0] == '15') { tr = 11; gs.log(tr);}



  else if (cr[0] == '16') { tr = 12; gs.log(tr);}



  else if (cr[0] == '17') { tr = 13; gs.log(tr);}



  else if (cr[0] == '18') { tr = 14; gs.log(tr);}



  else if (cr[0] == '19') { tr = 15; gs.log(tr);}



  else if (cr[0] == '20') { tr = 16; gs.log(tr);}



  else if (cr[0] == '21') { tr = 17; gs.log(tr);}



  else if (cr[0] == '22') { tr = 18; gs.log(tr);}



  else if (cr[0] == '23') { tr = 19; gs.log(tr);}



      return tr;



};



hr(current.opened_at.toString());


View solution in original post

4 REPLIES 4

BALAJI40
Mega Sage

So if you want return the hours from date time stamp,


simply do with split operation (did not going with glidedatetime functions)


suppose your date filed is, date = '2017-05-20 12:05:10;



var date1 = date.split(' ');


var timeStamp = date1[1].split(':');


var hours = timeStamp[1]; // gives the hours of date time format


johnnyd54
Giga Expert

I found this somewhere else on the community but I can't find where. It basically   just grabs the hour from the field and assigns it a number (tr) that goes in the corresponding bucket. I had to modify it from the original a little because of the time zones but the original had if cr[0] == '00', tr =0.



var hr = function(x){



  var br = x.split(' ');



  var cr = br[1].toString().split(':');



  var tr = 1;



  if (cr[0] == '00') { tr = 20; gs.log(tr);}



  else if (cr[0] == '01') { tr = 21; gs.log(tr);}



  else if (cr[0] == '02') { tr = 22; gs.log(tr);}



  else if (cr[0] == '03') { tr = 23; gs.log(tr);}



  else if (cr[0] == '04') { tr = 0; gs.log(tr);}



  else if (cr[0] == '05') { tr = 1; gs.log(tr);}



  else if (cr[0] == '06') { tr = 2; gs.log(tr);}



  else if (cr[0] == '07') { tr = 3; gs.log(tr);}



  else if (cr[0] == '08') { tr = 4; gs.log(tr);}



  else if (cr[0] == '09') { tr = 5; gs.log(tr);}



  else if (cr[0] == '10') { tr = 6; gs.log(tr);}



  else if (cr[0] == '11') { tr = 7; gs.log(tr);}



  else if (cr[0] == '12') { tr = 8; gs.log(tr);}



  else if (cr[0] == '13') { tr = 9; gs.log(tr);}



  else if (cr[0] == '14') { tr = 10; gs.log(tr);}



  else if (cr[0] == '15') { tr = 11; gs.log(tr);}



  else if (cr[0] == '16') { tr = 12; gs.log(tr);}



  else if (cr[0] == '17') { tr = 13; gs.log(tr);}



  else if (cr[0] == '18') { tr = 14; gs.log(tr);}



  else if (cr[0] == '19') { tr = 15; gs.log(tr);}



  else if (cr[0] == '20') { tr = 16; gs.log(tr);}



  else if (cr[0] == '21') { tr = 17; gs.log(tr);}



  else if (cr[0] == '22') { tr = 18; gs.log(tr);}



  else if (cr[0] == '23') { tr = 19; gs.log(tr);}



      return tr;



};



hr(current.opened_at.toString());


Ok, so that worked. However, I also need to adjust for time zones. Mine it appears to be 3 hrs off, I am -7 GMT in California. How did you modify for GMT? I'm thinking you could change the tr equals to a higher number.


Yes all I did was change the tr values. I'm eastern so I think you just need to subtract 3 from every tr value. Where I have tr = 0, you would change to 21 I think and so on