How To Find Day Of Week Using Apex?

How To Find Day Of Week Using Apex?

In this blog, you will learn how you can find day of week using apex.

This script first creates a Date object using the System.Today() method / String, which takes the year, month, and day as arguments. Then, it uses the format method of the Date object to get the day of the week in the form of a string. The format method takes a pattern string as an argument, and the pattern 'EEEE' specifies that the day of the week should be returned.

Finally, the script uses the System.debug() method to print the day of the week to the debug log.


Sample Code:

// Using System.Date()
Datetime today = System.today();
string dayOfWeek = today.format('EEEE');
System.debug('Day of week : ' + dayOfWeek);

// Using String Variable
string strDate = '2022-12-31';
Date dateValue = Date.valueOf(strDate);
Datetime tempDate = (Datetime) dateValue;
string dayOfWeek = tempDate.format('EEEE');
System.debug('Day of week : ' + dayOfWeek);

Demo

Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0