如何只获得当前日期

时间:2016-03-16 08:28:23

标签: c#

我有dd,mm,yyyy的3个下拉列表。 如何限制用户输入大于当前日期的日期。 2015年5月5日。 虽然给予输入7可能2015年给出无效日期。如何限制用户不要选择大于当前日期的下拉项

 protected void btnTest_Click(object sender, EventArgs e)
        {
            DateTime date1 = DateTime.Now;
            string dayNumber = date1.ToString("dd",
                              CultureInfo.InvariantCulture);

            string MonthNumber = date1.ToString("MM",
                             CultureInfo.InvariantCulture);


            string YearNumber = date1.ToString("yyyy",CultureInfo.InvariantCulture);

            if (DDLDay.SelectedItem.Text != "DD" && DDLMonth.SelectedItem.Text != "MM" && DDLYear.SelectedItem.Text != "YYYY")
            {
                if (Convert.ToInt32(DDLYear.SelectedItem.Text) <= Convert.ToInt32(YearNumber))
                {
                    if ((Convert.ToInt32(DDLMonth.SelectedItem.Value) <= Convert.ToInt32(MonthNumber)))
                    {

                        if ((Convert.ToInt32(DDLMonth.SelectedItem.Value) <= Convert.ToInt32(MonthNumber)))
                        {

                        }
                        else
                        {

                            Response.Write("not valid day ");
                            return;
                        }
                    }


                    else
                    {
                        Response.Write("not valid day");
                        return;
                    }
                }
                else
                {

                    Response.Write("not valid day ");
                    return;
                }


            }

            else
            {
                Response.Write("DOB Cannot blank");
            }




        }



  string currentDate = DateTime.Now.ToString("d");

2 个答案:

答案 0 :(得分:2)

从您当前的代码中,您可以轻松构建一个cvtColor(processedImage[x], gray_image, CV_BGR2GRAY); blur(gray_image, gray_image, Size(3, 3)); Canny(gray_image, canny_output, thresh, thresh * 2, 3); findContours(canny_output, contours, hierarchy, CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); cv::Moments mom = cv::moments(contours[0]); cv::HuMoments(mom, hu); // now in hu are your 7 Hu-Moments 对象并进行比较:

DateTime

您可以使用内置DateTime selectedDate = new DateTime( Convert.ToInt32(YearNumber) , Convert.ToInt32(MonthNumber) , Convert.ToInt32(DayNumber) ); if (selectedDate > DateTime.Now) { // error } 以统一日期格式。它还支持最大选择日期。

答案 1 :(得分:2)

为什么不在两个日期(>>=)之间直截了当userInput(或limit):

protected void btnTest_Click(object sender, EventArgs e) {
  DateTime limit = DateTime.Now.Date;  

  DateTime userInput = new DateTime(
    int.Parse(DDLYear.SelectedItem.Text),
    int.Parse(DDLMonth.SelectedItem.Text), 
    int.Parse(DDLDay.SelectedItem.Text));  

  //TODO: it's unclear from the question if you want ">" or ">=", put right comparison
  if (userInput >= limit)  {
    Response.Write("not valid day ");

    return;
  }

  ...
}