Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I know that you get today's date like this: Date.today(). What would be the best solution to get yesterday's date?

share|improve this question

2 Answers 2

up vote 19 down vote accepted

Just use addDays with a negative value:

Date.today().addDays(-1)
share|improve this answer

You can use the addDays() functions as already mentioned, but SOQL has a Date Literal Already set up for this, and might be a bit more simple. You just simply have to query for a date of YESTERDAY

An example

list<Account> accounts = [Select Id From Account Where CreatedDate = YESTERDAY];

Here is the documentation for Date Literals. Although the addDays() works, this seems a bit more clean in some use cases.

http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.