我想将当前日期,月份和年份保存为三个整数。我不知道该怎么做。
int day;
int month;
int year;
答案 0 :(得分:10)
#include <ctime>
int main() {
time_t t = time(0); // current time: http://cplusplus.com/reference/clibrary/ctime/time/
struct tm * now = localtime(&t); // http://cplusplus.com/reference/clibrary/ctime/localtime/
// struct tm: http://cplusplus.com/reference/clibrary/ctime/tm/
int day = now->tm_mday;
int month = now->tm_mon + 1;
int year = now->tm_year + 1900;
}
答案 1 :(得分:0)
好的,因为你对它一无所知,你可以在这里找到它
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html
一切都很详细。
答案 2 :(得分:0)
David的答案应该可以解决问题,但这是另一个版本(取决于Windows平台)
#include <windows.h>
#include <stdio.h>
#include <iostream>
int main()
{
char key;
int day;
int month;
int year;
SYSTEMTIME st;
GetSystemTime(&st);
printf("%02d-%02d-%04d %02d:%02d:%02d.%03d\n", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
day = st.wDay;
month = st.wMonth;
year = st.wYear;
std::cin >> key;
return 0;
}
希望这有帮助!
-CCJ