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
sujay Paulsujay Paul 

date issue

Hi All,
I am running this below code:

Date d=Date.today();
system.debug('todyas date:'+d);
Datetime dt = (DateTime)d;
String dayOfWeek = dt.format('EEEE');
system.debug('day Of Week :'+dayOfWeek);


And got the debug log:
22:01:52:002 USER_DEBUG [2]|DEBUG|todyas date:2019-08-23 00:00:00
22:01:52:002 USER_DEBUG [5]|DEBUG|day Of Week :Thursday

Error Log
but 23rd August 2019 is Friday. 
Can any one tell me what is the issue?
sfdcDeveloper 12sfdcDeveloper 12
You're implicitly converting a DateTime from a Date; this ends up setting it to midnight GMT, which, for the Western hemispehere, results in the day being one day earlier when formatted for local time. Instead, use DateTime.now() to get the correct day:
DateTime now = DateTime.now(); 
String dayOfWeek = now.format('EEEE'); 
System.debug('****** now: ' + now + ' ' + dayOfWeek);

More answer can be found here (https://salesforce.stackexchange.com/questions/99098/date-formateeee-doesnt-give-day-of-week)
 
Ajay K DubediAjay K Dubedi
Hi Sujay,

Please take variable 'd' as datetime type instead of date and then run your code. For ex:      
DateTime d = Date.today();   // d as datetime.
    System.debug('todyas date:'+d);
    Datetime dt = (DateTime)d;
    String dayOfWeek = dt.format('EEEE');
    System.debug('day Of Week :'+dayOfWeek);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too. 

Thanks,
Ajay Dubedi
www.ajaydubedi.com