Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Proper way to remove 1970-01-01 from getLocalTime() ?

robby3
Tera Contributor

ServiceNow seems to store all 'time' columns as the 1970-01-01 + time.   So when I run this

var gdt = new GlideDateTime();

gdt.getLocalTime()

I get 1970-01-01 14:59:57

What are the best practices for removing the 1970-01-01?   I just want 14:59:57   (actually, I just want the hour and minute in separate variables, but that's a different question).

I can do a string replace to nuke 1970-01-01 but that feels like a kludge.   I can't figure out how you can specify the date format to GlideDateTime. setValueUTC(gdt, "HH:mm") does not seem to work... it returns blank.

So how would you do this?

Thanks!

-Robby

1 ACCEPTED SOLUTION

robby3
Tera Contributor

I was able to solve the issue - getNumericValue() is the key. It gets us milliseconds since unix epoc (which is 1970-1-1 00:00:00).



So we can convert that to hours/minutes with some simple math.  



  var gdt = new GlideDateTime();


  var gdtTime = new GlideDateTime(gdt.getLocalTime());


  var imsTime = gdtTime.getNumericValue();


  var imsInHour = 3600000;


  var imsInMinute = 60000;


  var iLocalHours = Math.floor(imsTime / imsInHour).toFixed(0);


  var iLocalMinutes = Math.floor((imsTime - (iLocalHours * imsInHour)) / imsInMinute).toFixed(0);




I don't think max int will be an issue since the number of milliseconds in 24 hours = 86,400,000.


View solution in original post

1 REPLY 1

robby3
Tera Contributor

I was able to solve the issue - getNumericValue() is the key. It gets us milliseconds since unix epoc (which is 1970-1-1 00:00:00).



So we can convert that to hours/minutes with some simple math.  



  var gdt = new GlideDateTime();


  var gdtTime = new GlideDateTime(gdt.getLocalTime());


  var imsTime = gdtTime.getNumericValue();


  var imsInHour = 3600000;


  var imsInMinute = 60000;


  var iLocalHours = Math.floor(imsTime / imsInHour).toFixed(0);


  var iLocalMinutes = Math.floor((imsTime - (iLocalHours * imsInHour)) / imsInMinute).toFixed(0);




I don't think max int will be an issue since the number of milliseconds in 24 hours = 86,400,000.