I have two separate fields Date and Time fields. I would like to convert these fields into seconds format.

I am using this seonds format to make google API call.


Field1: Date: 2/18/2020 Field2: Time: 9.00 AM

Expected: Field3: Seconds: 1582078857 or possible with Apex itself?

And also is there a way we can preserve same time for any timezone user?

1 Answer 1


If you're simply trying to get the epoch, you can use the Datetime.getTime() method.

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

Here's an anonymous script to get you started:

Datetime timestamp = Datetime.newInstance(
    Date.newInstance(2020, 18, 2),
    Time.newInstance(9, 0, 0, 0)

USER_DEBUG [5]|DEBUG|1622649600000

  • but, date and time are two different fields. and also conversion changes on user timezone?
    – Ajay
    Feb 19, 2020 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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