How to store the current date in a struct?

1.2k Views Asked by At
struct date{
  int date;
  int month;
  int year
};

struct date current_date;

I have this struct. Then I wanna store the current date in current_date. How can I do this using C language?

Thank You so much!

2

There are 2 best solutions below

0
On BEST ANSWER

struct tm is a very standard structure for representing a broken down time representation with second resolution.

You would:

// get current time
time_t t = time(NULL);
struct tm *tm = localtime(&t);
// assign to your structure
current_date.date = tm->tm_mday;
current_date.month = tm->tm_mon + 1;
current_date.year = tm->tm_mon + 1900;
1
On

Hello in this source: https://www.geeksforgeeks.org/getdate-and-setdate-function-in-c-with-examples/

It says that getdate() function might be useful to you. It works like this:

 struct date yourdate; 
 getdate(&yourdate);  
 current_date.day = yourdate.da_day

And you should import #include <dos.h>