How Can I Tell the Day of the Week of a Date?

I’d like to build in some logic based on the weekday (e.g. Monday, Tuesday, Wednesday) of a given date. How can I identify the date, using either formulas or Apex code?

Answer

Formulas

There isn’t a built-in function to do this for you, but you… UPDATE: As of Spring 2018, there’s a built-in function for this: WEEKDAY(). It returns an integer where 1 is Sunday, 2 is Monday, 3 is Tuesday, etc.

Apex Code

You could do the same thing with time deltas, but you can also use the poorly documented DateTime.format() function:

// Cast the Date variable into a DateTime
DateTime myDateTime = (DateTime) myDate;
String dayOfWeek = myDateTime.format('E');
// dayOfWeek is Sun, Mon, Tue, etc.

Attribution
Source : Link , Question Author : Benj , Answer Author : Benj

Leave a Comment