質問

タグがあるDateオブジェクトに<ctime>を使用して現在の時間を取るために簡単な「初心者」方法はあります
int month
int day
int year
それのメンバ変数のための

?おかげます。

役に立ちましたか?

解決

time_t tt = time(NULL); // get current time as time_t
struct tm* t = localtime(&tt) // convert t_time to a struct tm
cout << "Month "  << t->tm_mon 
     << ", Day "  << t->tm_mday
     << ", Year " << t->tm_year
     << endl

tm構造体のint型はすべて0ベース(0 =月、1 = 2月)であり、あなたは、さまざまな日対策、月の日(tm_mday)、週(tm_wday)と年(tm_yday)。

を得ることができます

他のヒント

これはlocaltimeののリエントラント版であるため、

localtime_rがあるなら、あなたはの localtime_r のではなくlocaltimeのよりを使用する必要があります。

#include <ctime>
#include <iostream>

int main()
{
    time_t tt = time(NULL); // get current time as time_t
    tm  tm_buf;
    tm* t = localtime_r(&tt, &tm_buf); // convert t_time to a struct tm

    std::cout << "Month "  << t->tm_mon
              << ", Day "  << t->tm_mday
              << ", Year " << t->tm_year
              << std::endl;
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top