-1

Here is example how I do that in Linux, but in Windows I don't have strptime function anyone can help me solve this problem?

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

time_t to_seconds(const char *date)
{
    struct tm storage = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    char     *p       = NULL;
    time_t    retval  = 0;

    p = (char *) strptime(date, "%d-%b-%Y", &storage);
    if (p == NULL)
    {
        retval = 0;
    }
    else
    {
        retval = mktime(&storage);
    }
    return retval;
}

int main(int argc, char** argv) 
{
    time_t d1 = to_seconds("16-Jun-2015");
    time_t d2 = to_seconds("13-Jun-2015");
    if(d1 > d2)
    {
        printf("date 1 > date 2");
    }
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60

2 Answers2

1

If you happen to be using .Net, I'd suggest:

1) Use Convert() or DateTime.Parse()

EXAMPLE:

string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);       

2) Use DateTime.Compare() to compare the two DateTime values:

EXAMPLE:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
if (result < 0)
  Console.WriteLine("is earlier than");
else if (result == 0)
  Console.WriteLine("is the same time as";         
else
  Console.WriteLine("is later than";
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

I made function for this, easy to use...

https://github.com/vforv/date-compare-C

Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60
  • Link-only answer are not the type of answers preferred at SO. At least you want to paste here the relevant part of the code along with an explanation, why it is answeing the question. – alk Jun 28 '15 at 08:35