After converting to UNIX Time, the date shows 1970-01-20T18:30:45.400Z

Alon Grod
Tera Expert

I created PUT REST message and I need to pass some parameters to the json. one of the parameters is the field sys_created_on of type Date/Time on the incident table.
I need to convert this field to UNIX Time. What I did is:

var incCreated = current.open_at;
var gdt = new GlideDateTime(incCreated);
var unix = Math.floor(gdt.getNumericValue() / 1000);

 

The problem is that in the system gets the value as:

WhatsApp Image 2024-03-04 at 12.55.04.jpeg

 

Why the system gets 1970 and not the actual date (2024-03-04)? 

3 REPLIES 3

M Ismail
Tera Guru

Hi @Alon Grod,

The reason you're seeing the Unix epoch start date of January 1, 1970, is because the getNumericValue() method of the GlideDateTime object returns the number of milliseconds since the Unix epoch. When you divide this value by 1000 to convert it to seconds, you get a Unix timestamp. However, if the timestamp appears to be 1970, it indicates that the open_at field might not be correctly populated with a valid date/time value.

@M Ismail  how can I convert from unix back to regular date?

Sweet-Place9099
Tera Contributor

The date "1970-01-20T18:30:45.400Z" represents a specific moment in time in the UNIX Time format, also known as POSIX Time or Epoch Time. This format measures time as the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).

In this case, the timestamp "1970-01-20T18:30:45.400Z" corresponds to January 20, 1970, at 18:30:45 and 400 milliseconds (or 45.4 seconds) past UTC (Coordinated Universal Time)