How do I get the current date and time?


This question already has an answer here:

  • How to get current time and date in C++? 22 answers

How to get current date d/m/y. I need that they have 3 different variables not one, for example day=d; month=m; year=y;.

For linux, you would use the 'localtime' function.

#include <time.h>

time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);

int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900; // Year is # years since 1900