views:

236

answers:

3

Hello, is there a crossplatform way to get current date and time in c++? Thanks.

+5  A: 

std C libraries provide time()
This is seconds from the epoch and can be convverted to data and H:M:S using standard C functions. Boost also has a time/date library that you can check.http://www.boost.org/doc/libs/1_38_0/doc/html/date_time.html .

time_t  time;
time(&time)
Martin York
+1  A: 

Use ctime() together with Martins answer if you want a date string.

Magnus Skog
+2  A: 

C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date:

#include <ctime>
#include <iostream>
using namespace std;

int main() {
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    cout << (now->tm_year + 1900) << '-' 
         << (now->tm_mon + 1) << '-'
         <<  now->tm_mday
         << endl;
}
anon