How to compare day/month/year in C or C++?

Related searches

I have a program that asks the user to input to dates then it displays which one is more recent I've done it like this

if (year1>year2 || month1>month2 || day1>day2)
    return -1;

if (year1<year2 || month1<month2 || day1<day2)
    return +1;

but the output is not quite correct.

You need a much more complicated check than that:

if (year1 > year2)
    return -1;
else if (year1 < year2)
    return +1;

if (month1 > month2)
    return -1;
else if (month1 < month2)
    return +1;

if (day1 > day2)
    return -1;
else if (day1 < day2)
    return +1;

return 0;

NOTE: Returning -1 for first is greater than second seems counter-intuititive to me, however I have followed the semantics provided by the OP.

C program that compare two given dates. To store a date use a , C program that compare two given dates. To store a date use a structure that contains three members namely day, month and year. If dates are equal then� C program to print the current time, day, month, and year : In this C programming tutorial, we will learn how to print the current time, day, month, and year. This program will use time.h header file to print these values. The logic required to print these values is already implemented. You need to import time.h header file to use these

Here's a clean way to do it:

#include <tuple> // for std::tie

auto date1 = std::tie(year1, month1, day1);
auto date2 = std::tie(year2, month2, day2);

if (date1 == date2)
  return 0;

return (date1 < date2) ? -1 : 1; 

The comparisons of std::tie objects are lexicographical, so this returns -1 if date1 is less than date2, 0 if they are the same, and 1 if date1 is greater than date2.

You might be better off defining your own date type (or use boost::datetime).

struct Date
{
  unsigned year;
  unsigned month;
  unsigned day;
};

bool operator<(const Date& lhs, const Date& rhs)
{
  return std::tie(lhs.year, lhs.month, lhs.day) < 
         std::tie(rhs.year, rhs.month, rhs.day);
}

bool operator>(const Date& lhs, const Date& rhs) { .... }

bool operator==(const Date& lhs, const Date& rhs) { .... }

int date_cmp(const Date& lhs, const Date& rhs) 
{
  // use operators above to return -1, 0, 1 accordingly
}

C Program to calculate the difference of two dates in years, months , The following is a C program to calculate the difference of two dates in years, months and days. Make sure that the start date is earlier than the end date. Enter start date (MM/DD/YYYY): 08/05/2001 Enter end date (MM/DD/YYYY): 08/20/2001 Difference: 0 years 00 months and 15 days. Option Strict On Module DateTimeComparison Private Enum DateComparisonResult Earlier = -1 Later = 1 TheSame = 0 End Enum Public Sub Main() Dim thisDate As Date = Date.Today ' Define two DateTime objects for today's date ' next year and last year Dim thisDateNextYear, thisDateLastYear As Date ' Call AddYears instance method to add/substract 1

This statement

if (year1>year2 || month1>month2 || day1>day2)
    return -1;

tests if any one of the three conditions is true. So, if year1 is higher than year 2, or month1 is higher than month2. Lets stop there. Consider

year1 = 2013, month1 = 12, day1 = 31;
year2 = 2014, month2 = 1, day1 = 1;

We know that, infact, year2 is a higher value, but what happens is

is year1 > year2? no
ok, but is month1 > month2? yes

This makes it look like the first year is a higher value, but it's not, it just a higher month value.

As you get further into C++ you'll find that it's a good idea to try and adopt a convention of making all your comparisons use a single operator (< or >), when you reach a point where you are working with operators you'll understand why.

if (year2 < year1)
    return 1;
// we reach this line when year1 <= year2
if (year1 < year2) // elimnate the < case
    return -1;

// having eliminated both non-matches,
// we know that by reaching point that both
// dates have the same year. Now repeat for
// the month value.
if (month2 < month1)
    return 1;
if (month1 < month2)
    return -1;

// year and month must be the same, repeat for day.
if (day2 < day1)
    return 1;
if (day1 < day2)
    return -1;
return 0; // exact match

DateTime::diff - Manual, As of PHP 5.2.2, DateTime objects can be compared using comparison operators . $c = new DateTime( '2014-04-20' ); The function below returns a single number of years, months, days, hours, minutes or seconds between the current� I have 2 fields one in mmm/yyyy format and other field in dd/mmm/yyyy format. Can I compare whether there is 1 year or more gap between them? Actually these will come in as strings. But when I am trying to convert mmm/yyyy string to DateTime it is taking its own value for Date part. Could someone please help, as I am new to programming in c#.

//You can try this

int lday,lmonth,lyear;
int nday,nmonth,nyear;
int lhour,lminute;
int nhour,nminute;
sscanf(New_Time,"%d-%d",&nhour,&nminute); //reads the numbers
sscanf(Last_Time,"%d-%d",&lhour,&lminute); //from the string
sscanf(New_Date,"%d-%d-%d",&nday,&nmonth,&nyear);
sscanf(Last_Date,"%d-%d-%d",&lday,&lmonth,&lyear);
//cout << "Last date: " << lday << "-" << lmonth << "-" << lyear  <<endl;
//cout << "New date: " << nday << "-" << nmonth << "-" << nyear  <<endl;

if(nyear>lyear)
    return 0;
if(nyear==lyear) {
    if(nmonth > lmonth)
        return 0;
    if (nmonth == lmonth) {
        if(nday > lday)
            return 0;
        if (nday == lday) {
            if( nhour > lhour)
                return 0;
            if( nhour == lhour) {
                if(nminute>lminute) {
                    //cout << "new time >= last time" << endl <<endl;
                    return 0;
                }
                else return 1;
            }
            else return 1;
        }
        else return 1;
    }
    else return 1;
}
else return 1;

C date and time functions, The C date and time functions are a group of functions in the standard library of the C Types, struct tm, broken-down calendar time type: year, month, day, hour, minute, second Compatibility of C and C++ � Comparison with Pascal. Consider the problem of comparison of two valid dates d 1 and d 2.There are three possible outcomes of this comparison: d 1 == d 2 (dates are equal), d 1 > d 2 (date d 1 is greater, i.e., occurs after d 2) and d 1 < d 2 (date d 1 is smaller, i.e., occurs before d 2).

How to get the previous & next day date from a given date in Java, Learn Servlet � Learn JSP � Learn JSTL � Learn C � Learn C++ � Learn MongoDB � Learn XML � Learn Python 2) Get the previous day date and the next day date from the given date 3) Compare two dates with each other and next day date of this one */ String fromDate = "2014-01-01"; //split year, month and days from the� how to compare month-year with DateParse. Ask Question Asked 8 years, 9 months ago. Active 4 years, 1 month ago. Viewed 26k times 9. 1. I have to check if a date

Is there a built-in API in C to get the day of year easily? I have a solution below, but I want to know if there is a better one: 1. creating a tm struct of 01/31/2013 00:00:00 2. get timestamp by mktime() 3. convert timestamp to struct tm by localtime() 4. get tm_yday from tm struct

Calculate the number of days, months, or years between two dates using Excel functions. For example, you can calculate age in years, months and days. You can also calculate the number of days between another date and today.

Comments
  • Right, your logic is wrong, and this has nothing to do with C++. Just think it through. What you need is a lexicographical comparison of year, month and day, in that order.
  • Have you tried writing out a few sample dates and trying the logic out yourself? Compare 2013-12-31, 2014-01-01.
  • Probably a good idea to show writing the whole thing in terms of a single comparison operator, probably operator <.
  • @kfsone Agreed, however the OP doesn't provide enough context for the test. So I provided a drop-in replacement.
  • It's nice that you've also provided a return for the case where the dates are identical.
  • @Rob Thanks. I think the OP has the -1 and +1 back-to-front, however. But I have followed the code he provided.