C++ || How To Get The Day Of The Week & The Week Day Name From A Date Using C++

The following is a module with functions which demonstrates how to get the day of the week and the week day name from a given date using C++.

The function demonstrated on this page uses Zeller’s congruence to determine the day of the week from a given date.


1. Week Day

The example below demonstrates the use of ‘Utils::getWeekday‘ to get the day of week for a given date. The following function returns a value from a DayOfWeek enum, which represents the day of the week for the given parameters.

The following are possible values that are returned from this function:


• 0 = Sunday
• 1 = Monday
• 2 = Tuesday
• 3 = Wednesday
• 4 = Thursday
• 5 = Friday
• 6 = Saturday


2. Week Day Name

The example below demonstrates the use of ‘Utils::getWeekdayName‘ to get the name of the day of week for the given date.

The following are possible values that are returned from this function:


• Sunday
• Monday
• Tuesday
• Wednesday
• Thursday
• Friday
• Saturday


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
👍 YesNo

Leave a Reply