C# 在asp.net中将日期与今天进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15474497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:57:30  来源:igfitidea点击:

Compare date with today in asp.net

c#asp.netvalidationdatetime

提问by Dawood Ahmed

i have a textboxwhich takes input date from user. now i want to make a validatorwhich checks either date is greater then today or not.

我有一个textbox从用户那里获取输入日期的数据。现在我想做一个validator检查日期是否大于今天。

i tried this link but it has some problems http://forums.asp.net/t/1116715.aspx/1

我试过这个链接,但它有一些问题http://forums.asp.net/t/1116715.aspx/1

if i give this date 25/03/2013it is correct but if give 01/04/2013, it says it is less then today.

如果我给出这个日期,25/03/2013它是正确的,但如果给出01/04/2013,它说它比今天少。

**

**

Update

更新

<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtReturnDate"
                                Display="Dynamic" ErrorMessage="Date should be greater then  today" ForeColor="Red"
                                Operator="GreaterThan" ValidationGroup="VI">Date should be greater then  today</asp:CompareValidator>

**

**

Please help me to solve this problem

请帮我解决这个问题

采纳答案by Dawood Ahmed

ok i have done this by

好的,我已经这样做了

CompareValidator1.ValueToCompare = DateTime.Today.ToString("MM/dd/yyyy");

回答by Aidan

It thinks 1/4/2013 is the 4th January. You should create a DateTime object using the new DateTime(Year, Month, Day) constructor an the comparisson will work correctly, i.e

它认为 1/4/2013 是 1 月 4 日。您应该使用 new DateTime(Year, Month, Day) 构造函数创建一个 DateTime 对象,比较将正常工作,即

var compareDate = new DateTime(2013,4,1)
bool afterToday = DateTime.Today < compareDate

回答by Jamiec

The problem is that 25/3/2013is unambiguosly 25th March 2013, however with the wrong culture settings, 01/04/13could be 4th january 2013which is indeed before today's date. I assume you thought you were entering 1st April 2013which would be after.

问题是这25/3/2013是明确的25th March 2013,但是如果文化设置错误,01/04/13可能4th january 2013确实是在今天的日期之前。我假设您认为您正在进入1st April 2013之后。

The solution is one of

解决方案是其中之一

  • Use an unambiguous date format when typing into your textbox (2013-01-04for 1st April)
  • Use a date selector component which exposes the actual date
  • parse the date in the way you expect it (dd/MM/yyyy)
  • 在文本框中输入时使用明确的日期格式(2013-01-044 月 1 日)
  • 使用显示实际日期的日期选择器组件
  • 以您期望的方式解析日期 ( dd/MM/yyyy)

The problem with asp:CompareValidatoris that it does not seem to understand that dates can be formatted differently, and uses just the ToShortDateStringvariant of a DateTimeto compare (whoever implemented this should be shot!). The solution according to this questionseems to be to use a CustomValidator

问题asp:CompareValidator在于它似乎不明白日期可以采用不同的格式,并且只使用ToShortDateStringa的变体DateTime进行比较(谁实现了这个都应该被枪杀!)。根据这个问题的解决方案似乎是使用CustomValidator

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = DateTime.ParseExact(txtDate.Text,"dd/MM/yyyy") > DateTime.Today 
}

回答by Surya Deepak

this kinds of date validations should be permormed on the client side..in my Application we have used the following code

这种日期验证应该在客户端进行。在我的应用程序中,我们使用了以下代码

convert: function (d) {
        /* Converts the date in d to a date-object. The input can be:
        a date object: returned without modification
        an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
        a number     : Interpreted as number of milliseconds
        since 1 Jan 1970 (a timestamp) 
        a string     : Any format supported by the javascript engine, like
        "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
        an object     : Interpreted as an object with year, month and date
        attributes.  **NOTE** month is 0-11. */
        return (
        d.constructor === Date ? d :
        d.constructor === Array ? new Date(d[0], d[1], d[2]) :
        d.constructor === Number ? new Date(d) :
        d.constructor === String ? new Date(d) :
        typeof d === "object" ? new Date(d.year, d.month, d.date) :
        NaN
    );

 isFutureDate: function (a) {
        var now = new Date();
        return (a > now) ? true : false;
    },

Now call the above functions like this (isFutureDate(convert("your form date value"))).

现在像这样调用上面的函数 (isFutureDate(convert("your form date value")))。

回答by Don David

Use below code to compare specified date with todays date

使用以下代码将指定日期与今天日期进行比较

string date = "01/04/2013";
                DateTime myDate = DateTime.ParseExact(date, "dd/MM/yyyy",
                                           System.Globalization.CultureInfo.InvariantCulture);
                if (myDate > DateTime.Today)
                {
                    Console.WriteLine("greater than");
                }
               else
                {
                 Console.WriteLine("Less Than");
                }