9

I am getting date into JSON as below :

date=1382166999

It seems that date has been returned in milliseconds format.

Can anyone please suggest the way to convert it into normal date format and vice versa ?

1 Answer 1

17

This is known as a "Unix Timestamp" (number of milliseconds since January 1st, 1970, 00:00:00 GMT). In Apex Code, you can use DateTime.newInstance(unixTimestamp) to return a DateTime that contains the formatted date/time value. In JavaScript, use new Date(unitTimestamp) to get the same result. To convert back, in Apex Code, use myDate.getTime(), and in JavaScript, use the same function: myDate.getTime().

2
  • I have used it as DateTime dt = DateTime.newInstance(1667277663000); system.debug(dt); but it is giving an error...Illegal integer Nov 1, 2022 at 5:16
  • @PARAGTALUKA That's because the number is too large. Make it a long first (suffix with an l): DateTime dt = DateTime.newInstance(1667277663000l); system.debug(dt);
    – sfdcfox
    Nov 1, 2022 at 5:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .