使用C#比较ASP.NET中的小时数 [英] comparing hours in ASP.NET using C#

查看:86
本文介绍了使用C#比较ASP.NET中的小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在尝试比较两个约会时间,如下所示:

Dear All,

I am trying to compare two date hours as below:

DateTime Login =Convert.ToDateTime(ds.Tables[1].Rows[0]["Login"]);
      DateTime Logout = Convert.ToDateTime(ds.Tables[1].Rows[0]["LogOut"]);
      int var = DateTime.Compare(Login,Logout);
      if (var > 3 )
      {
       lblCount.Text = "0";
      }


在这里,我实际上是在检查登录和注销之间的差异是否超过3小时,然后:


Here, I am actually checking if the difference between login and Logout is more than 3 hours then :

lblCount.Text = "0";


我认为这是不正确的.有人可以建议我如何比较3小时的日期吗?


I don''t think its correct. Can any one please suggest me how to compare the dates for 3 hours?

推荐答案

使用int var = Logout.Subtract(Login).Hours;


参考: http://www.victorchen.info/get-date-difference-in-c/ [ ^ ]
Use int var = Logout.Subtract(Login).Hours;


Ref:http://www.victorchen.info/get-date-difference-in-c/[^]


DateTime.Compare用于检查2个DateTime参数是否相等.对于这一点,您可能要考虑为场景使用Hour属性.一种高级方法是将Logout.Hour减去Login.Hour,然后检查结果是否大于3.请参考 ^ ].
DateTime.Compare is used to check if the 2 DateTime parameters are equal or not. For this one, you might want to consider using the Hour property for your scenario. A high level approach would be to subtract Logout.Hour to Login.Hour and the check if the result is greater than 3. Refer here[^] for details.


此处是您想要的解决方案...我从数据表中获取的字符串值就像您从数据表中获取的字符串值...
here the solution which u want...the string value which I taken that value like you taken from the datatable.....
string INDate = "2011-02-02 4:45:25";
string OutDate = "2011-02-02 12:33:33";
DateTime startDate = Convert.ToDateTime(INDate);
DateTime endDate = Convert.ToDateTime(OutDate);
TimeSpan diff = endDate.Subtract(startDate);
string FV = diff.TotalHours.ToString();


祝你好运.....


Best of Luck.....


这篇关于使用C#比较ASP.NET中的小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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