Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ get which day by input date

Tags:

c++

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.

like image 632
user1600289 Avatar asked Feb 16 '26 01:02

user1600289


1 Answers

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

like image 53
user1071979 Avatar answered Feb 18 '26 15:02

user1071979