I'm trying to display the Start date Concatenated with the End Date. To achieve this I've converted my date field to string and concatenating the string value.

It works fine but in both start date and End date field value return one day earlier.

For Example:

datetime StartDate= Date.Valueof(t.get('Start_Date__c'));

this above StartDate Print the correct date 2020-01-01 00:00:00

String strStartDate = StartDate.formatGMT('MM/dd/YYYY');

If i print strStartDate it return the value of one day before 12/31/2020.

  List <AggregateResult> ts=   [SELECT  End_Date__c,Start_Date__c FROM 
                                      Time_Sheet__c WHERE Contact__c = :loggedinUserId
                                      Group by Start_Date__c, End_Date__c

        List<timeSheetWrapper> tswrap = new List<timeSheetWrapper>();
        for(AggregateResult t : ts){
            timeSheetWrapper aWraper = new timeSheetWrapper() ; 
            aWraper.Id = t.Id;

            datetime StartDate= Date.Valueof(t.get('Start_Date__c'));  
            String strStartDate = StartDate.formatGMT('MM/dd/YYYY');

            datetime EndDate= Date.Valueof(t.get('End_Date__c')); 
            String strEndDate = EndDate.formatGMT('MM/dd/YYYY');

            aWraper.StartEndDate =  strStartDate + ' - ' + strEndDate; 

        return tswrap; 


    public Class timeSheetWrapper{
        public id Id {get;set;}
        public String StartEndDate {get;set;}


1 Answer 1


Use DateTime.format() to get the date as per user's time local zone.

As per the documentation

formatGmt(dateFormatString) Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.

This is why you are getting difference in the datetime value.

So you need to use

format(dateFormatString) Converts the date to the local time zone and returns the converted date as a string using the supplied Java simple date format. If the time zone cannot be determined, GMT is used.

So you can format like this,

String strStartDate = StartDate.format('MM/dd/YYYY');


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .