我想在我的 C++ 程序中显示当前日期和时间。我只是通过以下几行代码做到了这一点:
char date[9], time[9];
_strdate( date );
_strtime( time );
cout << "Current date and time: " << date << " " << time << endl;
这在 MS Visual C++ 2010 和 Dev C++ 4.9.9.2 中完美运行。但这在 Xcode 中不起作用!它说我正在使用未声明的标识符“strdate”和“strtime”。我已经包含了 iostream 和 ctime 库,但这没有帮助。有什么问题?提前致谢。
最佳答案
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);
}
如前所述,标准c/c++库中没有这样的函数。 您可以使用此替代方法。代码基于在 Internet 上找到的一些信息。我没有编译它。
关于c++ - 不能在 Xcode 中使用 _strdate (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11968403/