-1

So i'm trying to validate date in format DDMMYYYY.

string date = "12031996";

i want to be able to validate if what user entered is in this specific format. And also want to be able to check if the year is in range of 1980 - 2005.

I'm new to C# so please forgive if it sounds like a stupid question. Thanks :)

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Shrey
  • 93
  • 3
  • 5
  • 12
  • 1
    Have you not done any research? This is a very straightforward issue. Have you thought about breaking it down into individual steps and researching how to do each step? – mason Dec 07 '17 at 02:31
  • Welcome to Stack Overflow (SO). SO is not a forum, it's actually a Question and Answer site. That being the case, your first course of action should be to search for answers before attempting to ask a question. Secondly, questions should be specific in a way that there is only 1 actual question. Your question is actually 2 and should be broken down as such. – Erik Philips Dec 07 '17 at 02:50
  • Before asking more question please take a moment to read [How to ask a good question](https://stackoverflow.com/help/how-to-ask), as it will increase your chances of either finding and answer or asking it in a way that it can be answer quickly. – Erik Philips Dec 07 '17 at 02:52

1 Answers1

2
if (DateTime.TryParseExact(date, "ddMMyyyy", NULL, DateTimeStyle.None, out DateTime dateTime))
{
    int year = dateTime.Year;
    if (year >= 1980 && year <= 2005)
        //do something if in range
    else
        //do something otherwise
}
else
    //do something for invalid format.

To learn more about how to parse a date time string in C#, read this page on MSDN and this one.

lamandy
  • 962
  • 1
  • 5
  • 13