+ Start a Discussion
EulogioGalloEulogioGallo 

Date.today() returning yesterday's date.

I have a scheduler that runs every day @9am PST that sends out birthday emails.  However, when it ran this morning, it sent out emails to people who had birthdays yesterday.  I'm using Date.today() to grab the date, and the debug log shows that this returned yesterday's date.

09:00:01.0 (19039191)|VARIABLE_ASSIGNMENT|[10]|this|{"listDay":"0x638eaffe","monthDayCount":"0x38f524b0","startDate":"0001-12-30T00:00:00.000Z","todaysDate":"2017-02-09T00:00:00.000Z"}|0x3f7a5e67

Anyone have a clue why?  I checked my org's timezone settings as well as my user's settings, but everything is correctly set to 'Los Angeles'.  Am I missing something?

 
Tyler DahleTyler Dahle
I had an issue where dates in my apex controller were off because it was taking the base GMT time instead of the actual time and date, even with timezones correctly set. There are specific GMT related functions with the date and datetimes like valueOfGmt() and stuff that corrects this and gives the correct times for your timezone. Not sure if that is exactly what is happening, but hope that can help.
Roshni RahulRoshni Rahul
Hi,

Salesforce schedules the class for execution at the specified time. Actual execution may be delayed based on service availability.

To solve your issue,
 Date.today().addDays(1);

Hope it helps.

Regards,
Roshni

 
EulogioGalloEulogioGallo
Hi Roshni,

Thanks for replying!  I've tried a variety of things, but I'm still having the same issue.  Here are all the different ways I've tried to get an accurate date:
 
Date todaysDate = DateTime.now().addHours(7).date();
Date dateTimeGMT = DateTime.now().dateGMT();
Date testTodaysDate = Date.today();
DateTime testDateTime = DateTime.now();
DateTime testDateTime2 = DateTime.now().addHours(7);
Date test2TodaysDate = DateTime.now().addHours(7).date();  // trying again to see if time changes

And here is how these variables are initialized when the Scheduler runs:
10:15:00.0 (24894244)|VARIABLE_ASSIGNMENT|[15]|this|{"dateTimeGMT":"2017-05-11T00:00:00.000Z","listDay":"0x1c1868b1","monthDayCount":"0x68ab5a57","startDate":"0001-12-30T00:00:00.000Z","test2TodaysDate":"2017-05-11T00:00:00.000Z","testDateTime":"2017-05-11T16:58:24.478Z","testDateTime2":"2017-05-11T23:58:24.478Z","testTodaysDate":"2017-05-11T00:00:00.000Z","todaysDate":"2017-05-11T00:00:00.000Z"}|0x5db2ba5e

You'll notice it isn't even returning today's date, but last week's!  Any help with this would be greatly appreciated, I'm really frustrated with this problem!​
Sebastian BecerraSebastian Becerra

Hello Eulogio. 

I'm having the exact same issue.

Did you ever found a way of fixing this? Thanks!

DheeruDheeru
Hello all,
 is this issue solved ? i m facing the same issue, can any one please help here?
RaviChandra GadiRaviChandra Gadi
Hello All,

Even I have faced the same issue with System.today() incorrect output in Scheduler class even after checking the time zone of Org. But I got the solution in mycase with the below approach,

I had scheduler class with a Date variable which is declared at the starting (outside the execute method). And I had scheduled it for a daily run at 4:00 AM PST on 23rd September 2020 for one week. But the problem is System.today() is printed 23rd September the whole week.
So I have put a debug inside the execute method to print my Date variable to see what is happening
Before:

global class ABC_Scheduled Implements Schedulable
    {
        Date Todaysdate = System.today(); // Which is there initially

        global void execute(SchedulableContext sc)
        {
            System.debug('Date1...'+Todaysdate ); // To check in the debug to see output in daily run
            System.debug('Todasdate direct system debug....'+System.today() ); // Added next day to check if there will be any change
After adding the last line which is System.today() in direct debug, I could see this output is CORRECT. So as per my understanding when we schedule the scheduler, for the first run it getting called from the scratch and Date variable is getting saved somewhere and printing the same value whole week (From the second run it is just running the execute method).
So i changed my logic little bit, I declared "Date Todaysdate" from outside execute method and placed it inside execute and used it. Then my problem got solved.
After change:

global class ABC_Scheduled Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            Date Todaysdate = System.today(); // Changed
            System.debug('Todaysdate'+Todaysdate );
Hope this helps :)

Regards,
Ravi Gadi.