-3

I want the apex equivalent of the following javascript method :

 var utcDate3 = Date.UTC(2019, 7, 19, 18, 20, 5);

Because I need the number of milliseconds in the given Date object since January 1, 1970, 00:00:00, universal time, but implemented in apex.

7
  • 5
    I'm curious as to what research you did prior to asking this question.
    – Derek F
    Aug 19, 2019 at 15:16
  • Ditto. But it was a 30sec answer, so I helped out. Aug 19, 2019 at 15:17
  • I check documentation on datetime methods, and there is no in-built method to get time elapsed from January 1, 1970, 00:00:00 UTC in apex
    – vanessen
    Aug 19, 2019 at 15:22
  • 3
    I've downvoted this for lack of research, because searching for any of "January", "milliseconds", "1970" in the datetime documentation would have quickly turned up the answer.
    – Derek F
    Aug 19, 2019 at 16:17
  • 4
    The answer to the question is clearly stated in the documentation
    – vanessen
    Aug 29, 2019 at 6:52

1 Answer 1

6

Per this document, you can use get the getTime() method for this.

getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

In your case,

DateTime utcDate3 = DateTime.newInstanceGMT(2019, 7, 19, 18, 20, 5);
Long myMiliseconds = utcDate3.getTime();
4
  • It gives error in JSconsole. Aug 19, 2019 at 15:16
  • 1
    OP wanted Apex, not JS. :D Aug 19, 2019 at 15:16
  • 1
    ` var utcDate3 = Date.UTC(2019, 7, 19, 18, 20, 5);` is javascript syntax, crap Aug 19, 2019 at 15:17
  • 1
    yups.. I misread Aug 19, 2019 at 15:18

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