Get current datetime in UTC

nehayadav
Kilo Contributor

Hi All,

Is it possible to get the current datetime in UTC?

I have a scenario where I have to find the difference a datetime value from my custom table field(this value is in UTC) and the current datetime. i want to make sure that the current datetime is also in UTC so that no matter what the timezone of the system is, the difference i get is always between two datetime values in UTC.

I am planning to use GlideDateTime subtract(GlideDateTime start, GlideDateTime end) to get the difference.

thanks,

Neha

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Neha,



GlideDateTime and the GlideDateTime.subtract don't need to worry about timezones.   All GlideDateTimes are internally stored as an integer representing the number of milliseconds since January 1, 1970 00:00:00 GMT.   So when you use the GlideDateTime subtract function, Time Zones are already factored out.   To get the current time in GlideDateTime, all you need is:



var gdt1 = new GlideDateTime(); // Current DateTime


var duration = GlideDateTime.subtract(gdt1, gdt2); // Assuming gdt2 is another GlideDateTime




Kind regards,



Travis


View solution in original post

4 REPLIES 4

russell_miller
Kilo Guru

Try this...



  var d = new Date();


  var n = d.getTime();


  alert(n)



RM


Thanks Russell, i am gonna try it and see if it works.


tltoulson
Kilo Sage

Hi Neha,



GlideDateTime and the GlideDateTime.subtract don't need to worry about timezones.   All GlideDateTimes are internally stored as an integer representing the number of milliseconds since January 1, 1970 00:00:00 GMT.   So when you use the GlideDateTime subtract function, Time Zones are already factored out.   To get the current time in GlideDateTime, all you need is:



var gdt1 = new GlideDateTime(); // Current DateTime


var duration = GlideDateTime.subtract(gdt1, gdt2); // Assuming gdt2 is another GlideDateTime




Kind regards,



Travis


Thanks for you response Travis, this worked



-Neha