Convert seconds to Days, Minutes and Seconds

后端 未结 4 609
走了就别回头了
走了就别回头了 2021-01-02 09:41

Hey everyone. I\'ve continuing to learn C++ and I\'ve been set the \'challenge\' of converting seconds to format as the Days,Minutes and Seconds.

For example: 316000

4条回答
  •  有刺的猬
    2021-01-02 10:25

    this seems to me to be the easiest way to convert seconds into DD/hh/mm/ss:

    #include 
    #include 
    using namespace std;    
    
    time_t seconds(1641); // you have to convert your input_seconds into time_t
    tm *p = gmtime(&seconds); // convert to broken down time
    
    cout << "days = " << p->tm_yday << endl;
    cout << "hours = " << p->tm_hour << endl;
    cout << "minutes = " << p->tm_min  << endl;
    cout << "seconds = " << p->tm_sec << endl;
    

    I hope it helps!

    Regards,

    Stoycho

提交回复
热议问题