如何在C#中检查两个比我大的字符串日期 [英] How Can check two string date in c# which one i greater than

查看:229
本文介绍了如何在C#中检查两个比我大的字符串日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何检查和比较日期,月份和年份,最后只输出输出DeactivationDate比仅输出输出的ActivationDate差很多.


how to check and compare both date and Month and year and finally only come output DeactivationDate is greter than ActivationDate that only come on output.

string strCon = GetConnectionString();
          OleDbConnection objCon = new OleDbConnection(strCon);
          string objcmd = "select TblBill.SlNo,TblBillNew.EmployeeNo,TblBill.EmployeeName,TblBill.CardNo,TblBill.DepartmentName,left(TblBill.DeactivationDate,10)as DeactivationDate,TblBill.DeactivationTime from TblBill LEFT JOIN TblBillNew ON TblBill.CardNo=TblBillNew.CardNo where  TblBill.DeactivationDate <>'' And TblBill.DeactivationDate > TblBill.ActivationDate;";
          objCon.Open();
          OleDbDataAdapter objAdpter = new OleDbDataAdapter(objcmd, objCon);
          DataTable dt = new DataTable();
          objAdpter.Fill(dt);
          objCon.Close();
          return dt;

推荐答案

答案是:不要. > 比较字符串始终按字符串顺序工作:
The answer is: don''t.
Comparing strings always works in string order:
1
10
11
...
2
20
21
...

对于任何实际用途而言,这通常是毫无用处的.

而是将日期存储为Date或DateTime字段-这样,您就可以直接比较它们,仅输入有效值,然后将它们作为日期返回给外部代码以按照用户的要求进行格式化:您无需查看它每次,并将其与用户当前的首选显示设置进行比较.

Which is generally pretty useless for any practical purpose.

Instead, store your dates as Date or DateTime fields - that way you can compare them directly, enter only valid values, and return them as dates to your external code for formatting as the user wants: you don''t need to look at it each time and compare what it might be against the users currently preferred display settings.


您可以使用TimeSpan对象将日期与另一个日期相减. TimeSpan以整数格式给出输出.

You can use a TimeSpan object to store the subtraction of a date with another. TimeSpan gives the output in integer format.

TimeSpan ts=new TimeSpan();
DateTime dt1=Convert.ToDateTime(date1);
DateTime dt2=Convert.ToDateTime(date2);

ts=dt1.Subtract(dt2);//Result in integer format
if(ts>0)
{
Response.Write("dt1 is greater than dt2");
}
else
{
Response.Write("dt1 is less than or equal to dt2");
}



-Anurag



-Anurag


这篇关于如何在C#中检查两个比我大的字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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