Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to check if date1 is before date2 in C

1 Answer

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

int main() {
    struct tm date1 = {0}, date2 = {0};
    time_t time1, time2;

    // Set date1 to 2025-06-01
    date1.tm_year = 2025 - 1900; // Year since 1900
    date1.tm_mon = 6 - 1;        // Month (0-11)
    date1.tm_mday = 1;           // Day of the month

    // Set date2 to 2025-06-15
    date2.tm_year = 2025 - 1900;
    date2.tm_mon = 6 - 1;
    date2.tm_mday = 15;

    // Convert to time_t
    time1 = mktime(&date1);
    time2 = mktime(&date2);

    // Compare the two dates
    if (time1 < time2) {
        printf("date1 is before date2\n");
    } else if (time1 > time2) {
        printf("date1 is after date2\n");
    } else {
        printf("date1 is the same as date2\n");
    }

    return 0;
}



/*
run:

date1 is before date2

*/

 



answered Jun 8, 2025 by avibootz

Related questions

1 answer 59 views
1 answer 62 views
1 answer 67 views
1 answer 61 views
1 answer 65 views
1 answer 65 views
...