Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ day of week for given date

Tags:

c++

date

weekday

I am trying to write a simple program in c++ that returns the day of the week for a given date.

The input format is day, month, year. I cannot get it to work with leap years. I tried subtracting one from the a variable when the input year is a leap year, but the program just ends up crashing without an error message.

I would appreciate any suggestions, but please try to remain simple, I am still a beginner. Apologies for the stupid question, and please excuse my mistakes, this is the first time I post on this site.

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;


int d;
int m;
int y;


string weekday(int d, int m, int y){
    int LeapYears = (int) y/ 4;
    long a = (y - LeapYears)*365 + LeapYears * 366;
    if(m >= 2) a += 31;
    if(m >= 3 && (int)y/4 == y/4) a += 29;
    else if(m >= 3) a += 28;
    if(m >= 4) a += 31;
    if(m >= 5) a += 30;
    if(m >= 6) a += 31;
    if(m >= 7) a += 30;
    if(m >= 8) a += 31;
    if(m >= 9) a += 31;
    if(m >= 10) a += 30;
    if(m >= 11) a += 31;
    if(m == 12) a += 30;
    a += d;
    int b = (a - 2)  % 7;
    switch (b){
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    case 3:
        return "Wednesday";
    case 4:
        return "Thursday";
    case 5:
        return "Friday";
    case 6:
        return "Saturday";
    case 7:
        return "Sunday";
    }
}

int main(){
    cin >> d >> m >> y;
    cout << weekday(d, m, y);
}
like image 877
OerllydSaethwr Avatar asked Nov 09 '16 22:11

OerllydSaethwr


People also ask

How do you calculate the day of the week from any date in C?

s = s + (date + year + (year / 4) - 2) ; and then s = s % 7. After the above process, s will contain an index to the week array which can be accessed as - week[s] and this value shows the day on the given date. Note: “%s” in printf() is used to print the string value of week[s].

What was the day of week?

According to international standard ISO 8601, Monday is the first day of the week. It is followed by Tuesday, Wednesday, Thursday, Friday, and Saturday. Sunday is the 7th and last day of the week.

What is the shortcut to find day from date?

Step1 :Take the first two digit of the given year. Step2 :Calculate the next highest multiple of 4 for the first two digit number. Step3 :Subtract 1 from the number. Step4 :Then, subtract the first two digit from the number.


1 Answers

First: Do not write your own function, if there already are standardized functions that can handle the same problem. Point is that you might easily make a mistake (and I can already see one in the first line of your weekday() function as it is now), whereas implementations of standardized functions have been tested thoroughly and you can be confident that they deliver the result you are expected to get.

That being said, here is a possible approach using std::localtime and std::mktime:

#include <ctime>
#include <iostream>

int main()
{
  std::tm time_in = { 0, 0, 0, // second, minute, hour
      9, 10, 2016 - 1900 }; // 1-based day, 0-based month, year since 1900

  std::time_t time_temp = std::mktime(&time_in);

  //Note: Return value of localtime is not threadsafe, because it might be
  // (and will be) reused in subsequent calls to std::localtime!
  const std::tm * time_out = std::localtime(&time_temp);

  //Sunday == 0, Monday == 1, and so on ...
  std::cout << "Today is this day of the week: " << time_out->tm_wday << "\n";
  std::cout << "(Sunday is 0, Monday is 1, and so on...)\n";

  return 0;
}
like image 85
Striezel Avatar answered Sep 17 '22 10:09

Striezel