首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在C++中获取当前时间和日期?

如何在C++中获取当前时间和日期?
EN

Stack Overflow用户
提问于 2018-02-07 00:05:48
回答 10查看 0关注 0票数 0

是否有支持跨平台的C++ 方法来获取当前日期和时间?

EN

Stack Overflow用户

发布于 2018-02-07 04:15:24

C++标准库没有提供默认的日期类型。C++从C继承了用于日期和时间操作的结构和函数,以及一些考虑到本地化的日期/时间输入和输出函数。

代码语言:javascript
复制
// Current date/time based on current system
time_t now = time(0);

// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;

// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
票数 0
EN
查看全部 10 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档