In Salesforce apex code, we can use DateTime class methods to convert date and time to required format. And in one such case we needed to get Datetime in epoch format, also known as Unix TimeStamp for saving date and time.
To get the epoch from a datetime variable we can use the getTime() instance method as following :
Datetime dt = Datetime.now(); Long l = dt.getTime(); system.debug(l);
The DateTime.getTime() method returns the number of milliseconds since Jan 1, 1970, so we’ll need to divide it by 1000 to get standard unix time, which is the number of seconds since the epoch.
Long epochDate = dt.getTime()/1000;
Store it as a Long type, and not an Integer type.
Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at email@example.com