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
Ryan O'SullivanRyan O'Sullivan 

Date Rolling back on it's own

I have a problem where a date field is being grabbed and if said date field is entered as the first day of the month, when it is read out on the visualforce component it reads out the previous month. 

I have it put out on the component as:

<tr>
 <td class="aboveHalf">
  <label class="custLabel bold">&nbsp; MONTH OF  {!reportDate}</label>
 </td>
</tr>

The code governing reportDate is:

    public String reportDate{
        get{
            if(reportDate == null){
                Datetime cDate = report.Reporting_Date__c;
                if(cDate != null){
                    reportDate = cDate.format('MMM yyyy');  
                }
            }
            return reportDate;
        }
        set;
    }
I've added code as a work around to add one day to the given date such that it bumps it up to the correct month. But I still feel this is a salesforce problem with their Date.format function. Am I wrong to assume this or is this a known bug?
Best Answer chosen by Ryan O'Sullivan
Naval Sharma4Naval Sharma4
The reason for this is all Date/Time fields are stored in the API as GMT time for data integrity purposes.
What happens when the data is imported in is it converts to the Time Zone and Locale settings of the User that is viewing the Date/Time field in a particular record each time.
The value in salesforce is stored in GMT but while viewing on the UI it adjusts the value to users time zone. Since users time zone is (GMT-04:00) Eastern Daylight Time (America/New_York), it is showing a day lesser. However in the database the value is populated correctly.

All Answers

Naval Sharma4Naval Sharma4
Hi Ryan,

I think you need to put your timezone while converting a datetime instance into string.

Check this out.
Datetime GMTDate = 
  Datetime.newInstanceGmt(2011,6,1,12,1,5);
String strConvertedDate = 
  GMTDate.format('MM/dd/yyyy HH:mm:ss', 
                 'America/New_York');
// Date is converted to 
// the new time zone and is adjusted
// for daylight saving time.
System.assertEquals(
  '06/01/2011 08:01:05', strConvertedDate);

 
Naval Sharma4Naval Sharma4
The reason for this is all Date/Time fields are stored in the API as GMT time for data integrity purposes.
What happens when the data is imported in is it converts to the Time Zone and Locale settings of the User that is viewing the Date/Time field in a particular record each time.
The value in salesforce is stored in GMT but while viewing on the UI it adjusts the value to users time zone. Since users time zone is (GMT-04:00) Eastern Daylight Time (America/New_York), it is showing a day lesser. However in the database the value is populated correctly.
This was selected as the best answer
Mahesh DMahesh D
Hi Ryan,

In a SOQL query you can specify either a particular date or a date literal. A date literal is a fixed expression that represents a relative range of time, such as last month, this week, or next year.
dateTime field values are stored as Coordinated Universal Time (UTC). When a dateTime value is returned in Salesforce, it’s adjusted for the time zone specified in your org preferences. SOQL queries, however, return dateTime field values as UTC values. If you want to process these values in different time zones, your application might need to handle the conversion.


Please check the below links:

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

https://help.salesforce.com/HTViewSolution?id=000004680&

https://paulforce.wordpress.com/2009/08/27/formatting-time-in-apex/


Please do let me know if it helps you.

Regards,
Mahesh