user3264174
user3264174

Reputation: 167

How to get the current DateTime formatted in C++ (like System.DateTime.Now in .NET)

How to get the current DateTime in C++ like in .NET (DateTime.Now) in this format : 20/10/2014 10:53:27 ?

I have found this library, but this cannot perform my vows.

ny brilliant idea, please ?

Upvotes: 2

Views: 6242

Answers (4)

Predelnik
Predelnik

Reputation: 5246

I guess you need std::time function.

Edit:

Here's example how to output current time in asked format:

 #include <ctime>
 #include <iostream>

 int main()
 {
     std::time_t t = std::time(NULL);
     char mbstr[100];
     if (std::strftime(mbstr, 100, "%d/%m/%Y %T", std::localtime(&t))) {
         std::cout << mbstr << '\n';
     }
 }

Upvotes: 3

Naszta
Naszta

Reputation: 7744

C++11: chrono?

std::chrono::time_point<std::chrono::system_clock>
    stamp(std::chrono::system_clock::now());

Upvotes: 0

cbacon
cbacon

Reputation: 23

I use this in my Project, hope it helps

time_t t;
t = time(NULL);

tm tlm;

localtime_s(&tlm, &t);
cout << tlm.tm_hour << tlm.tm_min ...

the struct tm is explained here: http://www.cplusplus.com/reference/ctime/tm/

Upvotes: 1

TNA
TNA

Reputation: 2745

Perhaps boost date-time is what you want.

Upvotes: 0

Related Questions