+ Start a Discussion

System.Today() conflict when logging in as another user

Hi All,

Just recently I came across one strange issue. I'm still investigating it. Posting the problem here in case anyone has faced the same issue earlier, or if I'm missing anything.
I'm an sys admin in GMT+5:30 timezone (India) and I've logged in on behalf of another user with GMT - 7:00 (US) and created a record (The logged-in users current date is 2016-06-14). The object field I'm checking is a DateTime field (holding the created datetime) and I noticed that when I extract the date part (using the <DateTimeField>.date() ) method, the date is "2016-06-13 00:00:00". However, when I'm simply printing out the dateTime field in debug (minus the .date() part), I'm getting "2016-06-14 00:00:00".

Due to this issue, the code is not working properly and I would appreciate if anyone can help me with this. I'm still investigating this and if anyone has faced similiar issues before, would really appreciate the inputs.

Amit VaidyaAmit Vaidya
Hi Ayan,

You needs to convert it into GMT time. For safer side also consider DayLight savings. May be following snippet will help you:
public static void updateDateTime(List<Task> checkTasks){
    Datetime expectedDate;
    for(Task t : checkTasks){
        if(t.ActivityDate != NULL){
            Datetime yourDate = datetime.newInstance(t.ActivityDate.year(), t.ActivityDate.month(), t.ActivityDate.day());
            Integer hour = 4;
            if(locateDate.format('zzzz').contains('Daylight') == true)
            hour = 3;
            expectedDate = datetime.newInstanceGmt(t.ActivityDate.year(), t.ActivityDate.month(), t.ActivityDate.day()+1, hour, 59, 00);
            t.Due_Date_Time__c = expectedDate;
Please let me know if this helps you.