简体   繁体   English

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

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

Hey thanks for dropping in. I just wan't to say i do apologize if anybody has came across this question before. 嘿,谢谢您的发言。我只是想说,如果有人以前曾遇到过这个问题,我对此表示歉意。

I have spend hours searching through the forums and google for a similar problems, but no luck so far. 我已经花了几个小时在论坛和Google上搜索类似的问题,但到目前为止还没有运气。

My intention for this program is to print out the elapsed time between two time which are in 24hr format. 我对该程序的意图是以24小时格式打印两次之间的经过时间。

As of now i think i only got my head around to convert the elapse 1st and 2nd "hh" into correct 24 time, but having trouble understanding how to do minutes and seconds. 截至目前,我认为我只是想将经过的第一和第二“ hh”转换为正确的24小时,但是却难以理解如何进行分钟和秒操作。

I really appreciate any guidance, it would be really helpful. 我真的很感谢任何指导,这将非常有帮助。 cheers. 干杯。

    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;
    }

Don't take the difference between hours, minutes and seconds. 不要把小时,分钟和秒之间的差异。 Convert both times to seconds since midnight and take the difference between these. 将两个时间都转换为自午夜以来的秒数,并取两者之间的差。 Then convert back to hh:mm:ss. 然后转换回hh:mm:ss。

By the way: there are structs and functions in time.h that can help. 顺便说一句: time.h中有可以帮助的结构和功能。

Assuming that the times are in the same day(same date) the best way to solve you problem is by converting the times to a seconds from midnight format like: 假设时间在同一天(同一日期),则解决问题的最佳方法是将时间从午夜格式转换为秒,例如:

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

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

long deference = fabs(seconds1 - seconds2);

then convert the deference back to h:m:s format like; 然后将引用转换回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