如何在使用C#的ASP.NET中以12小时格式找到两次之间的差异 [英] How can I find difference between two times in 12 hours format in ASP.NET with C#

查看:75
本文介绍了如何在使用C#的ASP.NET中以12小时格式找到两次之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望找到

两次之间的差异

小时差异..



什么我试过了:



 checkouttime = Convert.ToDateTime(ds.Tables [0] .Rows [0] [Check_InTime ]的ToString()); 
string time1 = checkouttime.ToShortTimeString();
string time = DateTime.Now.ToShortTimeString();



 DateTime dtcheckout = DateTime.Parse(time1); 
DateTime dtnow = DateTime.Parse(time);

TimeSpan span =(dtcheckout - dtnow);

解决方案

为什么要将DateTime值转换为字符串,然后将它们转换回DateTime以便与它们一起使用?

将它们保留为DateTime,并且只有在需要将它们呈现给用户时才转换为字符串。



既然你没有告诉我们你遇到了什么问题 - 代码应该有效,而且正是你如何找到差异 - 我必须假设它就是这样:

为什么你在一个名为< pre> checkouttime< / pre> 的变量中读取登记时间?

 checkouttime = Convert.ToDateTime(ds.Tables [0] .Rows [0] [Check_InTime]。ToString()); 

我怀疑你刚才使用错误的列...


i Want to Find

difference between two times

the difference in hour..

What I have tried:

checkouttime =  Convert.ToDateTime(ds.Tables[0].Rows[0]["Check_InTime"].ToString());
           string time1 = checkouttime.ToShortTimeString();
           string time = DateTime.Now.ToShortTimeString();


DateTime dtcheckout = DateTime.Parse(time1);
            DateTime dtnow = DateTime.Parse(time);

            TimeSpan span = (dtcheckout - dtnow);

解决方案

Why are you converting DateTime values to strings, in order to then convert them back to DateTime in order to work with them?
Keep them as DateTime and only ever convert to string when you need to present them to the user.

And since you don't tell us what problem you are meeting - the code should work and is exactly how you find the difference - I have to assume that it's this:
Why are you reading the check-in time into a variable called <pre>checkouttime</pre>?

checkouttime =  Convert.ToDateTime(ds.Tables[0].Rows[0]["Check_InTime"].ToString());

I suspect you are just using the wrong column ...


这篇关于如何在使用C#的ASP.NET中以12小时格式找到两次之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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