Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date comparison to find which is bigger in C

Tags:

c

I want to know how to find which is bigger date using a c program

kindly help me out plz....

Thanks

like image 478
Vivekanandan Avatar asked Mar 12 '11 14:03

Vivekanandan


People also ask

How can I compare two dates in C?

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).

How do you compare dates in programming?

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.

How do you compare two date functions?

To handle equality comparison, we use the date object alongside the getTime() date method which returns the number of milliseconds. But if we want to compare specific information like day, month, and so on, we can use other date methods like the getDate() , getHours() , getDay() , getMonth() and getYear() .


1 Answers

You can use the difftime function:

#include <time.h>
#include <stdio.h>

int main(void) {
  time_t date1, date2;
  // initialize date1 and date2...

  double seconds = difftime(date1, date2);
  if (seconds > 0) {
    printf("Date1 > Date2\n");
  }

  return 0;
}

If your dates are not of type time_t, you can use the function mktime to convert them.

like image 152
João Silva Avatar answered Oct 28 '22 21:10

João Silva