Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date and time? [duplicate]

Tags:

c++

time

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;.

like image 993
Wizard Avatar asked Dec 01 '11 15:12

Wizard


1 Answers

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
like image 157
Petesh Avatar answered Oct 15 '22 11:10

Petesh