在.net中的日期之间进行比较 [英] compare between dates in .net

查看:37
本文介绍了在.net中的日期之间进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个日期之间进行比较.从这两个日期开始,我仅使用 ToShortDateString()提取日期组件,如下所示.现在的问题是当我比较两个日期时.它的抛出错误-

I want to compare between two dates. From both the dates, I am fetching only date component using ToShortDateString(), as shown below. Now the problem is when I'm comparing the two dates. Its throwing error --

运算符> =不能应用于string和string类型的操作数."

"Operator >= can't be applied to operands of type string and string."

DateTime srtdate = Convert.ToDateTime(allitem["StartDate"].Text.ToString());
DateTime srtdate = Convert.ToDateTime(allitem["StartDate"].Text.ToString());

 (DateTime.Now.ToShortDateString() >= srtdate.ToShortDateString()) 

我只需要比较日期部分,而不是日期和时间.

I need to compare date component only, NOT date and time together.

请提出替代方法.谢谢

致乔恩:-

To JON:-

(我仔细阅读了您的所有解释,并希望您能真正理解要表达的意思.只是为了澄清更多内容,最后做一遍检查,我将举一个例子.)我有一个Web界面,在其中输入XYZ名称的开始日期和结束日期(注意,我只能在此处输入日期,而不能输入时间).

(I went tyhrough all what you explained and understood hopefully what the point actually you trying to make. Just to clarify more and make a last check I ll show an example.) I have an web interface, where I give a start date and end date for a XYZ name (Note I can enter only date here, not time).

开始日期-2012年2月22日结束日期-2012年2月22日

现在在后端(代码)中,如果开始日期和结束日期与当前日期相同,或者当前日期在开始日期和结束日期之间,则我希望设置一个ACTIVE标志.我给出这样的条件:-

Now in back end (code), if Start date and End date is same as Current date OR current date is in between start and end date, I want a ACTIVE flag set or else not. I give the condition as this:-

if ((DateTime.Today >= strdate.Date) && (DateTime.Today <= enddate.Date))
                    lblCondition.Text = "CHECKED";

现在,当我调试代码时,

Now when I debug the code,

DateTime.Today strdate.Date 的值均为2/22/2012 12:00:00 AM.

Both DateTime.Today and strdate.Date gives the value as 2/22/2012 12:00:00 AM.

那么,乔恩,我的问题是:-今天"和日期"是否按照上述要求工作,其中仅使用日期部分.我希望会.

So, Jon my question is:- Would 'today' and 'date' work as per mentioned requirement, where only date component used. I hope it would.

非常感谢您之前所做的所有说明.

Thanks a lot for all your explanantion before.

推荐答案

为什么要完全转换为字符串表示形式?如果只想将日期部分与两个 DateTime 值进行比较,则只需使用

Why are you converting to a string representation at all? If you only want to compare the date parts to two DateTime values, just use the Date property on each of them:

if (x.Date >= y.Date)

Today 属性等效于 DateTime.Now.Date .

Date Today 都删除了时间部分,留下了午夜的时间.仍然具有能够表示时间的类型是不理想的,但这只是 DateTime API的工作方式:(

Both Date and Today strip off the time part, leaving a time of midnight. It's not ideal that you've still got a type which is capable of representing times, but that's just the way the DateTime API works :(

请注意,除非您真的是,否则通常应在Web应用程序中避免使用 DateTime.Now DateTime.Today ./em>使用系统默认时区作为日期边界来适应它.用户对今天"的想法可能与服务器的想法不同.

Note that you should usually avoid using DateTime.Now or DateTime.Today in web applications unless you're really comfortable with it using the system default time zone as the day boundary. The user's idea of "today" may not be the same as the server's.

除非您的目标是真正获得文本表示,否则应避免使用字符串转换.

You should avoid using string conversions unless your goal is really to get a text representation.

当然,另一种选择是使用我正在建立的日期/时间库,诺达时间,您可以在其中使用 LocalDate 类型-显然,这使您更清楚地只对日期而不是时间感兴趣.

Of course another alternative would be to use the date/time library I'm building, Noda Time, where you could use a LocalDate type - obviously that makes it clearer that you're only interested in the date and not the time.

由于OP似乎不相信 Date 确实确实忽略了时间分量,因此有一个示例:

As the OP seems unconvinced that Date really does ignore the time component, here's an example:

using System;

public class Test
{
    static void Main()
    {
        // Two DateTime values with different times but
        // on the same date
        DateTime early = new DateTime(2012, 2, 22, 6, 0, 0);
        DateTime late = new DateTime(2012, 2, 22, 18, 0, 0);

        Console.WriteLine(early == late); // False
        Console.WriteLine(early.Date == late.Date); // True
    }    
}

这篇关于在.net中的日期之间进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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