hello guys as you can see i have a method isdate with return type datetime and parameter string, what i wanted to do is to do is to check if inputdate is valid or not. may dateformat is these "MM/dd/yyyy"

ex. if user1 input = "2/29/19" in method isdate the "try" part will fetch the data and return

ex. if user1 input = "2/30/19" not valid "catch" part will fetch data and split the string "2","30","12" only "2/12" will be left, and format "MM/yy" and get the last day of that month (my date handler is textbox only to performed error input unlike datetimepicker you can only pick correct format date)

    private DateTime isDate(string inputDate)

        {
           DateTime dt;
           try
           {
               dt = DateTime.Parse(inputDate);
               return dt;
           }
           catch
           {
               string[] indate = inputDate.Split('/');
               dt = DateTime.Parse(indate[0]+"/"+indate[2]);
               DateTime newdate = new DateTime();//dt.Year, dt.Month, 1);
               newdate.AddMonths(1).AddDays(-1);
               string formater = "MM/dd/yyyy";
               return DateTime.Parse(newdate.ToString(formater));
           }

        }

Change line 13 to dt = DateTime.Parse(indate[0] + "/01/"+indate[2]);.
Change line 14 to `DateTime newdate = dt.AddMonths(1).AddDays(-1);
Remove line 15.

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.