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
Kyle RobertsKyle Roberts 

Date/DateTime operations

Is there a simpler way to deal with dates and times in apex other than calculating based on milliseconds? I've found the date/datetime operations to be quite gimped in Apex. I appreciate the ability to add days, seconds etc.. Where are the subtract and comparison methods? I'm coming from a C# background, bare with me here.

 

I have various things i need to accomplish, some are very straightforward.. Others are not. 

 

Checking whether a date/datetime is:

 

- Before another date/datetime

- Less/more than N days before a date/datetime

- Is before a certain time (and how to get the delta between the two, IE in the form of a TimeSpan or equivalent..)

 

Please any help would be appreciated, i do not have any code examples to provide. I am hoping for an elegant solution that will not steer me even further away from salesforce.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

someDateTIme.addHours(-10);

 

There's no supplied equivalent of TimeSpan, for comparing dateTimes, you can use < and >

All Answers

Kyle RobertsKyle Roberts

Thanks for the response, however i've seen this documentation. I'm sort of looking the same set of functions only instead of add, subtract? It's apparent they probably do not exist in the apex framework but i'm sure the community has faced similar situations. I hope someone has some insight on how to more easily compare the dates and times. Most important to me is finding the delta between two dates other than in the form of milliseconds.

 

-Kyle

 

SuperfellSuperfell

someDateTIme.addHours(-10);

 

There's no supplied equivalent of TimeSpan, for comparing dateTimes, you can use < and >

This was selected as the best answer
Kyle RobertsKyle Roberts

Thanks, the ms delta will have to suffice.