【问题标题】:Save Current Date into 3 Ints - C++ [closed]将当前日期保存为 3 个整数 - C++ [关闭]
【发布时间】:2011-11-25 02:52:02
【问题描述】:

我想将当前日期、月份和年份保存为三个整数。我不知道该怎么做。

int day;
int month;
int year;

【问题讨论】:

  • 当我需要它时,我的“不(研究)努力”关闭原因在哪里......
  • @Jonathan:再次阅读该关闭原因的描述。这可能是我们唯一适用的原因,但这不是正确的原因。
  • 这不应该被关闭,只能被否决。
  • @Xeo 我不同意。这个问题显然不完整。

标签: c++ date


【解决方案1】:
#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;
}

上面的链接

【讨论】:

  • 非常感谢!不知道如何使用 ctime 或任何这些东西。 C++ 非常非常非常新手
【解决方案2】:

好吧,你对它一无所知,你可以在这里找到它

http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html

所有细节。

【讨论】:

    【解决方案3】:

    上面大卫的回答应该可以解决问题,但这是另一个版本(取决于 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

    参考:http://www.cplusplus.com/forum/beginner/48115/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多