0

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.

https://maps.googleapis.com/maps/api/directions/json?
origin=Brooklyn&destination=Queens
&departure_time=1343641500&mode=transit
&key=YOUR_API_KEY

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

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

getTime()
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)
);
system.debug(timestamp.getTime());

USER_DEBUG [5]|DEBUG|1622649600000

| improve this answer | |
  • but, date and time are two different fields. and also conversion changes on user timezone? – Ajay Feb 19 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.