0

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));
           }

        }

Edited by auwi987: sa

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Momerath
0

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.

`

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.