Yashman
Yashman

Reputation: 111

can't use _strdate in Xcode (c++)

I want to show current date and time in my c++ program. I did this simply by these lines of code:

char date[9], time[9];

_strdate( date );
_strtime( time );

cout << "Current date and time: " << date << " " << time << endl;

This perfectly worked in MS Visual C++ 2010 and Dev C++ 4.9.9.2. But this doesn't work in Xcode! It says that I'm using undeclared identifiers 'strdate' and 'strtime'. I have included iostream and ctime libraries, but this didn't help. What's the problem? Thanks in advance.

Upvotes: 0

Views: 1819

Answers (3)

cubuspl42
cubuspl42

Reputation: 8390

std::string _strdate_alternative()
{
 char cptime[50];
 time_t now = time(NULL);
 strftime(cptime, 50, "%b. %d, %Y", localtime(&now)); //short month name
 return std::string(cptime);
}

As said before, there is no such function in standard c/c++ library. You can use this alternative. Code is based on some info found on the Internet. I didn't compile it.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409266

The function you are looking for is strftime.

Upvotes: 0

eq-
eq-

Reputation: 10096

These functions are not part of standard C++; they are Microsoft-specific extensions, and thus may not be available everywhere.

For similar functionality, you may try combining time, localtime and strftime.

Upvotes: 1

Related Questions