如何使用asp.net,C#比较两个日期 [英] How to compare two dates using asp.net, C#

查看:88
本文介绍了如何使用asp.net,C#比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string todaydate = DateTime.Now.ToString("dd/MM/yyyy");
if(txtDate.text<todaydate)>
{
    Response.Write("Supplied date is not Current Date");
}



为什么它不是运营商<或者>。我无法理解

谢谢


Why it is not except operator < or >. I m not able to understand
Thanks

推荐答案

不能使用日期字符串,使用结构实例 System.DateTime 。只有当您要在屏幕上显示数据时才会出现这些字符串(对于大多数典型情况,这些字符串都是HTML格式)。



DataTime的实例可与运营商'==','!=','<','>','< ='或'> ='进行比较。

您还可以减去两个时间点,以获得结构 System.TimeSpan 的实例,表示它们之间的时间距离。这种减法最可读的方法就是操作员' - '(减号)。如果你从较晚的时间减去结果为正的较早时间,则在相反的情况下为负。



请参阅:

http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system。 timespan.aspx [ ^ ]。



-SA
Do not work with date strings, work with the instance of the structure System.DateTime. Those strings should appear only when you about to display data on screen (in HTML, for most typical case).

The instances of DataTime can be compared with operators '==', '!=', '<', '>', '<=' or '>='.
You can also subtract two points in time to get an instance of the structure System.TimeSpan representing the distance in time between them. The most readable way of doing such subtraction is, no surprise, the operator '-' (minus). If you subtract earlier time from later time the result is positive, negative in the opposite case.

Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA


你可以这样做:

You could do it in this way:
string todaydate = DateTime.Now.ToString("dd/MM/yyyy");
DateTime dtSuppliedDate = DateTime.Parse(txtDate.Text);

if(dtSuppliedDate.Subtract(DateTime.Today).Days!=0)
{
    Response.Write("Supplied date is not Current Date");
}


请尝试谷歌。许多网站上有许多帖子用于比较C#,ASP.NET中的日期。





希望这个帮助。
Please try Google. There are many postings on many sites for comparing dates in C#, ASP.NET.


Hope this help.


这篇关于如何使用asp.net,C#比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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