function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Divya VetchamDivya Vetcham 

convert date to timestamp

Date Type
Khan AnasKhan Anas (Salesforce Developers) 
Hi Divya,

Greetings to you!

To get the timestamp from a datetime variable you can use the getTime() instance method as following :
Datetime dt = Datetime.now();
Long ts = dt.getTime();
system.debug(ts);

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;

Reference: https://blog.webnersolutions.com/salesforce-convert-datetime-to-epoch-format

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Divya,

Greetings to you!

Use the following procedure to get time stamp from datetime. It may helpful for you.


Long TimeInMiliseconds = DateTime.now().getTime(); 
Long MiliSecInLocalTimeZone = DateTime.now().millisecond();
Long timeStamp = (TimeInMiliseconds-MiliSecInLocalTimeZone);

we will get timeStamp in miliseconds. To get Epoch timestamp devide timestamp by 1000.
Long Epoch TimeStamp = timestamp/1000;
millisecond()
Return the millisecond component of a Datetime in the local time zone of the context user.

Use the following links to get any help related to this problem.

https://salesforce.stackexchange.com/questions/30550/datetime-now-gettime-not-returning-correct-value#comment42494_30554
https://www.epochconverter.com/?prefs
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com