3

How do I able to get which day by input date?

Input Date example: 15-08-2012

How do I know if its monday, tuesday or which day using C++.

I am trying to omit out weekends from the date available of a month, so If i input e.g the month of August 2012, i want to check which day is saturday and which day is sunday, so i can omit it out from the available date for my program.

Code that I tried for getting the amount of days in a month:

if (month == 4 || month == 6 || month == 9 || month == 11)
{
    maxDay = 30;
}
else if (month == 2)
//{
//  bool isLeapYear = (year% 4 == 0 && year % 100 != 0) || (year % 400 == 0);
//  if (isLeapYear)
//  { 
//   maxDay = 29;
//  }
//else
{
    maxDay = 28;
}

The next thing i want to know is in that month, which day are weekend so i can omit that from result.

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
user1600289
  • 59
  • 1
  • 1
  • 5
  • Not an exact duplicate, but more than sufficient to answer the question: [Correctness of Sakamoto's algorithm to find the day of week](http://stackoverflow.com/questions/6385190/correctness-of-sakamotos-algorithm-to-find-the-day-of-week) – Nemo Aug 15 '12 at 15:33
  • Is that your whole "days in month" code? What happens when month == 1? – Kevin Aug 15 '12 at 15:37
  • Do you have a 'tm' type variable you can work with? If so you could use strftime to get it into a word such as "monday" and then see if the day is == "sunday" || "saturday" and go from there. documentation: http://www.cplusplus.com/reference/clibrary/ctime/strftime/ – fooOnYou Aug 15 '12 at 15:39

5 Answers5

5
#include <ctime>

std::tm time_in = { 0, 0, 0, // second, minute, hour
        4, 9, 1984 - 1900 }; // 1-based day, 0-based month, year since 1900

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

// the return value from localtime is a static global - do not call
// this function from more than one thread!
std::tm const *time_out = std::localtime( & time_temp );

std::cout << "I was born on (Sunday = 0) D.O.W. " << time_out->tm_wday << '\n';

Date to Day of the week algorithm?

Community
  • 1
  • 1
user1071979
  • 1,701
  • 2
  • 12
  • 25
  • 3
    You can't reliably initialize a `tm` like that. The standard specifies the members that the structure has, but not their order. – James Kanze Aug 15 '12 at 15:50
3

I'd use mktime(). Given day, month and year, fill out a tm, then call mktime on it:

tm timeStruct = {};
timeStruct.tm_year = year - 1900;
timeStruct.tm_mon = month - 1;
timeStruct.tm_mday = day;
timeStruct.tm_hour = 12;    //  To avoid any doubts about summer time, etc.
mktime( &timeStruct );
return timeStruct.tm_wday;  //  0...6 for Sunday...Saturday
James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

Here is a simpler and probably better implementation as it does not need any extra library imports. The result returned is an int from 0 to 6 (Sunday, Mon, Tue... Saturday).

#include <iostream>

int dayofweek(int d, int m, int y){
    static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

/* Driver function to test above function */
int main(){
    int day = dayofweek(23, 10, 2013); 
    // Above statement finds the week day for 10/23/2013
    //dayofweek(<day in month>,<month>,<year>)
    std::cout << day;
    return 0;
}
Deekshith
  • 1,544
  • 2
  • 14
  • 15
1

You should use mktime and ctime and extract the tm_wday field of the tm structure. It is guaranteed that mktime doesn't require that field, so you can populate a skeleton tm structure, process it and decompose it back into a complete structure:

#include <ctime>

std::tm t = {};
t.tm_mday = 15;
t.tm_mon = 8;
t.tm_year = 2012;

std::tm * p = std::localtime(std::mktime(&t));

// result is p->tm_wday
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0
#include <stdio.h>
#include <time.h>

int main ()
{
  char *str = "15-08-2012";
  struct tm tm; 
  if (strptime (str, "%d-%m-%Y", &tm) == NULL) {
    /* Bad format !! */
  }
  char buffer [80];
  strftime (buffer, 80, "Day is %a", &tm);
  puts (buffer);    
  return 0;
}
perreal
  • 94,503
  • 21
  • 155
  • 181