-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.

closed as off-topic by vanessen, Lukas Lunow, Reshma, glls, Oleksandr Berehovskyi Aug 29 at 11:29

  • This question does not appear to be about Salesforce within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    I'm curious as to what research you did prior to asking this question. – Derek F Aug 19 at 15:16
  • Ditto. But it was a 30sec answer, so I helped out. – Sebastian Kessel Aug 19 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 at 15:22
  • 2
    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 at 16:17
  • 4
    The answer to the question is clearly stated in the documentation – vanessen Aug 29 at 6:52
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();
  • It gives error in JSconsole. – Pranay Jaiswal Aug 19 at 15:16
  • 1
    OP wanted Apex, not JS. :D – Sebastian Kessel Aug 19 at 15:16
  • 1
    ` var utcDate3 = Date.UTC(2019, 7, 19, 18, 20, 5);` is javascript syntax, crap – Pranay Jaiswal Aug 19 at 15:17
  • 1
    yups.. I misread – Pranay Jaiswal Aug 19 at 15:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.