如何计算两小时之间的时差HH:mm 12/24小时时间格式 [英] How calculated the time difference between two hours HH:mm 12/24 hour time format

查看:373
本文介绍了如何计算两小时之间的时差HH:mm 12/24小时时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在任何时候输入文本框来计算。编码应计算在文本框中输入的小时数。

I want to calculate any time I enter text boxes. The encoding should calculate the hours entered in the text boxes.


我也使用这样的代码

also I use the code like this

private void simpleButton14_Click(object sender, EventArgs e)
        {
            textEdit7.Text = SaatFarki("05:30", "22:30");
            
            textEdit7.Text = SaatFarki(textEdit4.Text, textEdit6.Text);
            
        }
        private string SaatFarki(string basla, string bitir)

        {
            DateTime time1 = DateTime.ParseExact(basla, "HH:mm", CultureInfo.CurrentCulture);
            DateTime time2 = DateTime.ParseExact(bitir, "HH:mm", CultureInfo.CurrentCulture);
            TimeSpan time3 = time2 - time1;
            return $"{time3.Hours:00} : {time3.Minutes:00}";

        }


我正在写第一个文本框19:15而且 我正在写第二个文本框02:30 


它在第三个文本框中显示如下结果-16:45

I'm writing to the first text box 19:15 and I'm writing in the second textbox 02:30 

it shows the result like this in third textbox -16:45


但是


例如


当我以这种方式输入文本框时,第一个texbox 02:30第二个文本框19:15

when I entered the textboxes this way, first texbox 02:30 second textbox 19:15


在第三个文本框中显示如下结果16:45 

shows the result like this in third textbox 16:45 


为什么上面的结果会显示一个减号? 下面应该是

why the above result shows with a minus sign? the below side that should be


推荐答案

Hello BurakTurhan,

Hello BurakTurhan,

处理时间时,需要在其中包含一些逻辑。当结果小于0时,您需要在结果中添加24小时。让我们说你要计算从10:00到14:00。如果你算数小时它是4小时吗?假设现在你有19:00
到凌晨2:00。如果你做了计算,你会有7个小时。如果你按照正常情况进行抽象,那么计算机将返回-17:00对吧?好了加24它就会得到7这就是你想要的。所以检查你是否有一个负面时间然后加24到
it。你的代码应如下所示:

When dealing with time you need to involve some logic in it. When your result is less than 0 you need to add 24 hours to your result. Lets say you want to calculate from 10:00 to 14:00. if you do the math it's 4 hours right ? Let's say now you have 19:00 to 2:00 am. If you make the calculations you will have 7 hours. If you do the abstraction like normal the computer will return -17:00 right? well add 24 to it and you will get 7 which is what you want. So check if you have a negative time and then add 24 to it. Your code should look like this:

private void simpleButton14_Click(object sender, EventArgs e)
        {
            textEdit7.Text = SaatFarki("05:30", "22:30");
            
            textEdit7.Text = SaatFarki(textEdit4.Text, textEdit6.Text);
            
        }
        private string SaatFarki(string basla, string bitir)

        {
            DateTime time1 = DateTime.ParseExact(basla, "HH:mm", CultureInfo.CurrentCulture);
            DateTime time2 = DateTime.ParseExact(bitir, "HH:mm", CultureInfo.CurrentCulture);
            TimeSpan time3 = time2 - time1;
            if (time3.TotalHours < 0)
            {
                 return


" {24 + time3.Hours:00}:{time3.Minutes :00}英寸;
}
return
"{24 + time3.Hours:00} : {time3.Minutes:00}"; } return


" {time3.Hours:00}:{time3.Minutes:00}" ;;

}
"{time3.Hours:00} : {time3.Minutes:00}"; }

希望这可以帮到你。

最好的问候,

Konstantinos Pap

Konstantinos Pap


这篇关于如何计算两小时之间的时差HH:mm 12/24小时时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆