-3
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
    Check out this [answer](https://stackoverflow.com/questions/5141960/get-the-current-time-in-c). `localtime` will help you get what you want – TSG Jul 23 '20 at 08:32
  • Your question is - how to get current date. Storing is obvious: `current_date.date = ; current_date.month = ; current_date.year = ;` – i486 Jul 23 '20 at 08:33
  • @i486 Yeah. What should I use for That's my qestion? – Kasun Jalitha Jul 23 '20 at 08:37
  • 1
    Does this answer your question? [Get the current time in C](https://stackoverflow.com/questions/5141960/get-the-current-time-in-c) – TSG Jul 23 '20 at 08:52
  • Under Windows call `GetSystemTime()` or `GetLocalTime()` to get current date in `SYSTEMTIME` structure. – i486 Jul 23 '20 at 11:36

2 Answers2

2

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;
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

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>

pigwidgeon
  • 190
  • 1
  • 2
  • 11
  • Huh? `dos.h`? I've checked [C11 Standard - 7.1.2 Standard headers(p2)](https://port70.net/~nsz/c/c11/n1570.html#7.1.2) and nowhere is `dos.h` listed. Where does that header come from? – David C. Rankin Jul 23 '20 at 08:54