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
Raj R.Raj R. 

How to get the day of the week from the DateTime field?

Hi,

I am using the following code below to get the day of the week from the DateTime field, but I am finding that the day of the week is wrong. Essentially i am getting the ((day of the week) - 1) 

For example:
  • For a DateTime value of 4/9/2016, i am getting Friday (when it is really on a Saturday)
  • For a DateTime value of 4/12/2016, i am getting Monday (when it is really on a Tuesday)
Any thoughts on how i can get the correct day of the week?
//for simplicity assume dt variable is defined and set to 2016-04-09 00:00:00. 
String dateStr = dt.format('EEEE'); 

//use for debugging
System.debug('Day of Campaign: '+dateStr+ ' Time: '+dt);

 
Best Answer chosen by Raj R.
geeta garggeeta garg
Hi

You can try this:-
Event et = [Select Id, StartDateTime From Event Limit 1];
getDayOfWeek(et.StartDateTime);


getDayOfWeek(DateTime dt) {
Datetime dt1 = DateTime.newInstance(dt, Time.newInstance(0, 0, 0, 0));
String dateStr = dt1.format('EEEE');
System.debug('Day of event: ' + dateStr);
}
Thanks

 

All Answers

Deepak GulianDeepak Gulian
Date myDate = Date.newInstance(2016, 4, 9);
Time myTime = Time.newInstance(0, 0, 0, 0);
Datetime dt = DateTime.newInstance(myDate, myTime);
String dayOfWeek=dt.format('EEEE');
System.debug('Day : ' + dayOfWeek);

 
Raj R.Raj R.
Hi Deepak,

Unfortunately, that is not working. Below you shall find another example of code that i am using. 
Event et = [Select Id, StartDateTime From Event Limit 1];

getDayOfWeek(et.StartDateTime);

//debug statement prints the day of event
getDayOfWeek(DateTime dt) {
         String dateStr = dt.format('EEEE');
          System.debug('Day of event: ' + dateStr);
}
Any other thoughts?
Vijay NagarathinamVijay Nagarathinam
Hi,

I think the time zone difference between you and organization time, that is the reason you are getting next day.

Let me know if you need any help regarding this.

Thanks,
Vijay
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello rRup, 

You can pass the timezone and then can try - something like - 
Event et = [Select Id, StartDateTime From Event Limit 1];

getDayOfWeek(et.StartDateTime);

//debug statement prints the day of event
getDayOfWeek(DateTime dt) {
         String dateStr = dt.format('EEEE', 'America/Los_Angeles');
          System.debug('Day of event: ' + dateStr);
}
You can have more information about the timezone here - 
http://help.salesforce.com/apex/HTViewHelpDoc?id=admin_supported_timezone.htm
You can go to the USER details and can find out in  which time zone he is - 
Pls, Let me know if further help is needed.

Thanks
Sumit Kumar Singh

 
geeta garggeeta garg
Hi

You can try this:-
Event et = [Select Id, StartDateTime From Event Limit 1];
getDayOfWeek(et.StartDateTime);


getDayOfWeek(DateTime dt) {
Datetime dt1 = DateTime.newInstance(dt, Time.newInstance(0, 0, 0, 0));
String dateStr = dt1.format('EEEE');
System.debug('Day of event: ' + dateStr);
}
Thanks

 
This was selected as the best answer
Raj R.Raj R.
Hi all,

Appreciate your feedback! All your ideas came together and helped me alot!