+ Start a Discussion
ismyhcismyhc 

Date showing 1 day behind for some reason when using date.format('yyyy-dd-MM')

I must be missing something..

 

The date is always 1 day behind when formating like so:

 

datetime xDate = o.campaign_start_date__c;
			
String xxDate = xDate.format('yyyy-dd-MM');
system.debug('sdate :' +xxDate);

 

I set the above field to 8/16/2011

 

and in debug it shows up as: 2011-15-08

 

Everytime I change it the debug value is shown as 1 day behind.. Im sure I could to +1, but could someone help me understand why this is happening?

 

Thanks!
Jacob Davis

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

This  is campaign_start_date__c  datetime field . And your users time zone must be such that GMT - some hours, so that GMT Offset makes a day less.

 

Try this

 

Datetime localTime = o.campaign_start_date__c.Date();
         
localTime = localTime.addHours(o.campaign_start_date__c.hour());
localTime = localTime.addMinutes(o.campaign_start_date__c.minute());
localTime = localTime.addSeconds(o.campaign_start_date__c.second());

String xxDate = xDate.format('yyyy-dd-MM');
system.debug('sdate :' +xxDate);

 

All Answers

Shashikant SharmaShashikant Sharma

This  is campaign_start_date__c  datetime field . And your users time zone must be such that GMT - some hours, so that GMT Offset makes a day less.

 

Try this

 

Datetime localTime = o.campaign_start_date__c.Date();
         
localTime = localTime.addHours(o.campaign_start_date__c.hour());
localTime = localTime.addMinutes(o.campaign_start_date__c.minute());
localTime = localTime.addSeconds(o.campaign_start_date__c.second());

String xxDate = xDate.format('yyyy-dd-MM');
system.debug('sdate :' +xxDate);

 

This was selected as the best answer
ismyhcismyhc

Thanks Shashikant!

 

Sorry, I forgot to follow up and let you know this worked for me. This was probably much better method that just adding a day to the current date.

 

Thanks!

Shashikant SharmaShashikant Sharma

Your welcome mate