4

I just want to compare these two date/time fields but the time is causing issues. How to remove the time or ignore it and compare them like if (prevStartDate == currStartDate)

Thanks

Date prevStartDate = 2016-05-29 14:02:00;
Date currStartDate = 2016-05-29 16:02:00;
  • Are the values of dates you're looking at stored in GMT but were created from different locations or are they Date-Times that you need to get the Date Values of? It appears to me to be the latter. Note that the dates are the same, only the times are different in the examples you're showing. – crmprogdev May 30 '16 at 14:22
6

If you want to compare Date-Times as Dates, just use prevStartDat.Date() and currStartDate.Date() to convert them to Date values. See the date-time class for more.

2

crmprogdev is correct. Here's an example.

// Create dummy date times
DateTime currentDate = DateTime.now();
DateTime previousDate = currentDate.addDays(-1);
// Set date times to dates
currentDate = currentDate.Date();
previousDate = previousDate.Date();
System.debug('Current Date: ' + currentDate);
System.debug('Previous Date: ' + previousDate);
System.debug(previousDate < currentDate);

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.