如何在 c++ 中获取当前日期
How can I get current date in c++?
我没有找到任何给出唯一日期的解决方案。我找到了解决方案,但一切都很复杂,我们必须解析数组并将日期与时间分开
尝试这样做
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}
如果你运行以下代码,那么今天你会发现当前日期采用以下格式。
15-04-14
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
char c[9];
_strdate_s(c);
cout<<c<<endl;
return 0;
}
ideone code
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "Year: "<< 1900 + ltm->tm_year << endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
}
相关文章:
- 获取当前正在运行的 exe 名称(不是路径)
- 使程序检查当前日期是否=预期日期
- 如何获取当前用户的默认RAS电话簿路径?
- 获取当前密钥状态?
- C++程序以获取当前时间
- 如何使用提升获取当前文件路径?
- 日期和当前日期之间的差异
- 如何在 c++ 中获取当前文件路径?
- 仅使用C++中的标准库获取以毫秒为单位的当前日期和时间
- 在 C++20 中获取当前日期/时间是否线程安全?
- 如何在 c++ 中获取当前日期
- Qt - 以 mmmm YYYY 格式获取当前日期(作为字符串)
- 获取C++的当前日期和时间
- 可视化如何在C++程序中获取当前日期
- 从时钟获取当前日期
- C++11 获取当前日期和时间作为字符串
- 如何获取当前日期和时间
- 使用localtime_s获取当前日期
- 获取当前日期和时间
- 如何以秒为单位获取系统的当前日期时间