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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Week Day // Declare data int day = 27; int month = 1; int year = 1991; std::cout << "Day = " << day << ", Month = " << month << ", Year = " << year << std::endl; // Get the weekday auto weekday = Utils::getWeekday(day, month, year); // Display result std::cout << "Weekday = " << weekday << std::endl; // expected output: /* Day = 27, Month = 1, Year = 1991 Weekday = 0 */ |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Week Day Name // Declare data int day = 27; int month = 1; int year = 1991; std::cout << "Day = " << day << ", Month = " << month << ", Year = " << year << std::endl; // Get the weekday name std::string weekdayName = Utils::getWeekdayName(day, month, year); // Display result std::cout << "Weekday Name = " << weekdayName << std::endl; // expected output: /* Day = 27, Month = 1, Year = 1991 Weekday Name = Sunday */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// ============================================================================ // Author: Kenneth Perkins // Date: Feb 5, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <string> #include <cmath> namespace Utils { // Specifies the day of the week enum DayOfWeek { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6 }; /** * FUNCTION: getWeekday * USE: Returns the day of week for the given parameters * @param day: The day * @param month: The month * @param year: The year * @return: The day of week for the given parameters * 0 = Sunday, 1 = Monday, 2 = Tuesday, ... 6 = Saturday */ DayOfWeek getWeekday(int day, int month, int year) { if (month == 1) { month = 13; --year; } if (month == 2) { month = 14; --year; } int q = day; int m = month; int k = year % 100; int j = static_cast<int>(std::floor(year / 100)); int h = static_cast<int>( q + (std::floor((13 * (m + 1)) / 5)) + k + (std::floor(k / 4)) + (std::floor(j / 4)) + (5 * j) ); h = ((h + 6) % 7); return static_cast<DayOfWeek>(h); } /** * FUNCTION: getWeekdayName * USE: Returns the name of the day of week for the given weekday * @param weekday: The day of the week * @return: The name of the day of week for the given weekday */ std::string getWeekdayName(DayOfWeek weekday) { std::string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; return days[weekday]; } /** * FUNCTION: getWeekdayName * USE: Returns the name of the day of week for the given parameters * @param day: The day * @param month: The month * @param year: The year * @return: The name of the day of week for the given parameters */ std::string getWeekdayName(int day, int month, int year) { return getWeekdayName(getWeekday(day, month, year)); } }// http://programmingnotes.org/ |
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// ============================================================================ // Author: Kenneth Perkins // Date: Feb 5, 2021 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { // Declare data int day = 27; int month = 1; int year = 1991; display("Day = " + std::to_string(day) + ", Month = " + std::to_string(month) + ", Year = " + std::to_string(year)); // Get the weekday auto weekday = Utils::getWeekday(day, month, year); display("Weekday = " + std::to_string(weekday)); // Get the weekday name std::string weekdayName = Utils::getWeekdayName(day, month, year); display("Weekday Name = " + weekdayName); } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
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.
Leave a Reply