如何获取当前日期和时间
How to get current date and time?
如何获取当前日期d/m/y。我需要它们有3个不同的变量而不是一个,例如day=d; month=m; year=y;
对于linux,您将使用'localtime'函数。
#include <time.h>
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900; // Year is # years since 1900
这是chrono
方式(c++ 0x) -查看它在http://ideone.com/yFm9P
#include <chrono>
#include <ctime>
#include <iostream>
using namespace std;
typedef std::chrono::system_clock Clock;
int main()
{
auto now = Clock::now();
std::time_t now_c = Clock::to_time_t(now);
struct tm *parts = std::localtime(&now_c);
std::cout << 1900 + parts->tm_year << std::endl;
std::cout << 1 + parts->tm_mon << std::endl;
std::cout << parts->tm_mday << std::endl;
return 0;
}
ctime库提供这样的功能。
也检查这个。这是另一个帖子,可能会帮助你根据你的平台。
相关文章:
- 使程序检查当前日期是否=预期日期
- 日期和当前日期之间的差异
- 使用 C++将时间戳转换为格式化的日期时间
- C++代码的 C# 日期时间等效项是什么?
- 如何在提升日期时间中创建日期范围?
- 仅使用C++中的标准库获取以毫秒为单位的当前日期和时间
- 在 C++20 中获取当前日期/时间是否线程安全?
- 以C++/提升表示 ISO 8601 持续时间,具体取决于当前日期
- 标准::时间 将天数添加到当前日期
- Poco 日期时间格式化程序 - 使用当前时区打印时间戳
- 符合C++的实现如何指示它不知道当前日期和时间?
- 如何在当前日期/时间之后命名我创建的文本文件
- TDateTime变量中的当前日期/时间
- 获取C++的当前日期和时间
- C++11 获取当前日期和时间作为字符串
- 如何获取当前日期和时间
- 当前日期和时间为字符串
- c++不能在当前系统时间和日期之后命名txt文件
- 获取当前日期和时间
- 如何以秒为单位获取系统的当前日期时间