user1600289
user1600289

Reputation: 59

C++ get which day by input date

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.

Upvotes: 3

Views: 19847

Answers (5)

Deekshith
Deekshith

Reputation: 1584

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;
}

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477140

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

Upvotes: 1

James Kanze
James Kanze

Reputation: 153929

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

Upvotes: 3

perreal
perreal

Reputation: 97958

#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;
}

Upvotes: 0

user1071979
user1071979

Reputation: 1791

#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?

Upvotes: 5

Related Questions