[SalesForce] Date.Format(‘EEEE’) doesn’t give day of week

When I try the following:

Date now = Date.today();
String dayOfWeek = now.format('EEEE');
System.debug('****** now: ' + now + ' ' + dayOfWeek);

I get the following error:

Method does not exist or incorrect signature: [Date].format(String)

However when I write it like so:

Datetime now = Date.today();
String dayOfWeek = now.format('EEEE');
System.debug('****** now: ' + now + ' ' + dayOfWeek);

I get no error but the output is wrong (the day of week should be Wednesday):

|DEBUG|****** now: 2015-11-11 00:00:00 Tuesday

How can I get the correct dayOfWeek for the date, I want to use date because I do not need the time. I have verified that my timezone etc is correct in my salesforce settings. I want to stay away from odd 'hacks'

Any help would be greatly appreciated!

Best Answer

Try this:

Datetime now = Date.today(); 
now = now.addHours(1); 
now.format('EEEE');

Reason: You are type casting Date to DateTime. Date does not have Time component so formatting behave incorrectly.

Related Topic