繁体   English   中英

两个时间之间以24小时格式表示的经过时间hh:mm:ss

[英]Elapsed time between two time in 24hr format hh:mm:ss

嘿,谢谢您的发言。我只是想说,如果有人以前曾遇到过这个问题,我对此表示歉意。

我已经花了几个小时在论坛和Google上搜索类似的问题,但到目前为止还没有运气。

我对该程序的意图是以24小时格式打印两次之间的经过时间。

截至目前,我认为我只是想将经过的第一和第二“ hh”转换为正确的24小时,但是却难以理解如何进行分钟和秒操作。

我真的很感谢任何指导,这将非常有帮助。 干杯。

    int main()
    {
        char time1[ 48 ] = "14:48:34";
        char time2[ 48 ] = "02:18:19";

        int hour1;
        int min1;
        int sec1;

        int hour2;
        int min2;
        int sec2;

        int seconds;
        int temp;

        //time1
        hour1 = atoi( strtok( time1, ":" ) );
        min1 = atoi( strtok( NULL, ":" ) );
        sec1 = atoi( strtok( NULL, ":" ) );

        //time2
        hour2 = atoi( strtok( time2, ":" ) );
        min2 = atoi( strtok( NULL, ":" ) );
        sec2 = atoi( strtok( NULL, ":" ) );

        seconds = hour1 * 60 * 60; //convert hour to seconds
        ...

        system("PAUSE");
        return 0;
    }

不要把小时,分钟和秒之间的差异。 将两个时间都转换为自午夜以来的秒数,并取两者之间的差。 然后转换回hh:mm:ss。

顺便说一句: time.h中有可以帮助的结构和功能。

假设时间在同一天(同一日期),则解决问题的最佳方法是将时间从午夜格式转换为秒,例如:

long seconds1 = (hours1 * 60 * 60) + (minutes1 * 60) + seconds1;

long seconds2 = (hours2 * 60 * 60) + (minutes2 * 60) + seconds2;

long deference = fabs(seconds1 - seconds2);

然后将引用转换回h:m:s格式,例如;

int hours = deference / 60 / 60;

int minutes = (deference / 60) % 60;

int seconds = deference % 60;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM