简体   繁体   English

我如何从2个下拉列表中获取值并将其作为C#中的日期进行比较

[英]How can i get values from 2 dropdown lists and compare them as a date in C#

I have 2 dropdown lists that show months and year. 我有2个下拉列表,分别显示月份和年份。 I can validate it and check if its less than a certain date in JS but now i wanna do this server side but i cannot figure out how to do pass the selected values and convert them to a date function. 我可以验证它,并检查它是否小于JS中的某个日期,但是现在我想在服务器端进行操作,但是我无法弄清楚如何传递所选值并将它们转换为日期函数。

UPDATE:

This is my HTML/ASP.Net 这是我的HTML / ASP.Net

 <asp:CustomValidator ID="CustomValidatorED" runat="server" 
 ErrorMessage=" *Enter a valid Expiry Date" Display="Dynamic" CssClass="error"
 ClientValidationFunction="ValidateED" 
 OnServerValidate="CustomValidatorED_ServerValidate"></asp:CustomValidator>

This is my Code Behind which is incomplete C#:- 这是我的背后的代码,它是不完整的C#:-

protected void CustomValidatorED_ServerValidate(object source, ServerValidateEventArgs args)
{
    var x = EDY.SelectedValue;
      var y = EDM.SelectedValue;

      var z = x + y;
     var pppp = Convert.ToInt32(z);
     DateTime entereddate = new DateTime(pppp);

      DateTime d = DateTime.Now;
      var r = d.Month;
      var p = d.Year;

      var kkkk = r + p;

      DateTime todaysdate = new DateTime(kkkk);

      int result = DateTime.Compare(entereddate, todaysdate);

      if (result < 0)
      { args.IsValid = true; }
      else
      { args.IsValid = false; }

}

`EDY = Years Drop Down`
`EDM = Months Drop Down`

I know i have made some obvious mistakes but this function is being called even when the date is correct, so if the entered date is less than todays date, it should show error but not the other times. 我知道我犯了一些明显的错误,但是即使日期正确,此函数也会被调用,因此,如果输入的日期小于今天的日期,则应该显示错误,而其他时间不会。

Mainly just want to fix my codebehind to get it working properly as im comparing entered MM/YYYY and todays MM/YYYY 主要是想修正我的代码以使其正常工作,因为我比较输入的MM / YYYY和今天的MM / YYYY

DateTime dtime1 = new DateTime(Convert.ToInt32(ddl1.SelectedValue));
DateTime dtime2 = new DateTime(Convert.ToInt32(ddl2.SelectedValue));

if(dtime1.equals(dtime2))
   doSomething();
//Conversion to comparing Date

DateTime date1 = dtime1.Date;
DateTime date2 = dtime2.Date;

The code above does exactly as you are wondering. 上面的代码确实可以满足您的要求。 Allows you to compare two values from your drop down lists and compare them as DateTimes. 允许您比较下拉列表中的两个值并将它们比较为DateTimes。

Something like this. 这样的事情。 You may have to be concerned about how you are converting the date (ie using a format speficiation) if you see that your date comparison is not working in respect to time etc. 如果您发现日期比较在时间等方面不起作用,则可能需要担心如何转换日期(即使用格式特殊化)。

protected void CustomValidatorED_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime currentDate = DateTime.Now;

        DateTime chosenDate = Convert.ToDateTime(args.Value);

        if (currentDate <= chosenDate)
        { args.IsValid = true; }
        else
        { args.IsValid = false;}

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个列表并使用C#相应地设置像素值? - How can I compare two Lists against each other and set the pixels values accordingly using C#? 如何比较两个列表并在c#中找到不匹配的列表? - How to compare two lists and find the mismatch them in c#? 如何在C#中从SQL正确获取日期? - How I can get date correctly from sql in c#? 如何在C#中从此响应中提取并获取值 - How can I extract and get the values from this response in C# 如何在 C# 中比较包含 json 值的 2 个列表以进行单元测试 - How to compare 2 lists containing json values in C# for unit testing 比较3个列表之间的值,并将第一个列表中的缺失值添加到其余2个列表中-C# - Compare values between 3 lists and add missing values from first list into the remaining 2 lists - C# UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum 如何从其他 class 获取帐号和密码并在 c# 中查看登录屏幕 - How can I get the account number and password from the other class and check them for the login screen in c# C#我有两个列表。 我想比较一下 - C# I have two lists. I want to compare them 如何比较不同的日期值 - How can I compare different date values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM