0

We have date for example:

char date[100] = "04/05/1900";

In excel you can convert it to integer. For example 04/05/1900 = 125.

There is a library or function in C that converts the date to an integer and vice versa?

This is for my program in University, task which I should make in C.

char date[100] = "04/05/1900";
int number = 125;
int convertDatoToInteger(date);
char convertIntegerToDate(number);

I expect the output of 04/05/1900 to be 125. And the output of 125 to be 04/05/1900.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Possible duplicate of [C Library for Parsing Date Time](https://stackoverflow.com/questions/1414155/c-library-for-parsing-date-time) – Federico klez Culloca Jul 26 '19 at 14:24
  • Welcome to StackOverflow. Please notice that even if there wasn't a duplicate, it's still off-topic to ask for us to recommend a library. Take a look at ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask) – Federico klez Culloca Jul 26 '19 at 14:25
  • 3
    Finally, if it's homework for uni, your instructor probably wants you to manually parse the date, not to rely on a library. – Federico klez Culloca Jul 26 '19 at 14:26
  • [How to find leap year programatically in C](https://stackoverflow.com/questions/3220163/how-to-find-leap-year-programatically-in-c) isn't a solution but might get you started. – Weather Vane Jul 26 '19 at 14:31
  • 1
    The usual way to do this sort of thing in C is to fill in a `struct tm` with your year, month, and day, then call the library function [`mktime`](https://linux.die.net/man/3/mktime) (or maybe [`timegm`](https://linux.die.net/man/3/timegm)). But the number this gives you counts seconds (not days), and since 1970-01-01, not 1899-12-01. (Is that really the base date Excel uses?) But I suppose you could divide by 86400, and add 25598... – Steve Summit Jul 26 '19 at 14:59
  • 1
    If you do end up doing anything with `struct tm`, beware that its definitions of "year" and "month" are peculiar. `tm_mon` is 0-based, and `tm_year` is 1900-based. (But `tm_mday` is fine.) – Steve Summit Jul 26 '19 at 15:11
  • Beware also that `struct tm` and `mktime()` (and `timegm()`) deal in dates *and times*, meaning that complications like time zones and daylight saving time come into play. If you're only intereted in dates, it's generally safest to set `tm_hour` to 12, and `tm_min` and `tm_sec` to 0. (Also set `tm_isdst` to 0 or -1.) – Steve Summit Jul 26 '19 at 17:10
  • 1
    @Dmitry Flow Note Excel has a bug in dates. Try `"02/28/1900"`. `"02/29/1900"`. `"03/01/1900"`. [incorrectly considered 1900 to be a leap year](https://en.wikipedia.org/wiki/Leap_year_bug). Do you want that buggy behavior too? – chux - Reinstate Monica Jul 26 '19 at 18:59

0 Answers0