1

I am slowly learning to do some simple console applications with C# and have now faced an issue what I do not manage to solve. I've tried searching for a solution all over the Stack Overflow / Internet, but either I don't know how to properly search for it, or there is no answer to what I'm seeking.

Situation: I am creating a simple console app that asks the following things from user: First name, Family name, Age.

Every prompt (question) has been introduced to the user via the following code:

System.Console.Write("What is your date of birth? ");
String dob = System.Console.ReadLine();

I have made a simple checker for the names, which seeks if they're between 1-30 characters and if they are, the application writes the results in a text document.

Question: How can I check if the date of birth has been written in the following format: DD.MM.YYYY?

6
  • 5
    DateTime.TryParseExact?
    – Jon Skeet
    Jul 6, 2016 at 10:32
  • Can you show me some sort of an example of the usage with Strings? I am a novice when it comes to C#. Thanks in advance!
    – Pepelius
    Jul 6, 2016 at 10:33
  • Related : stackoverflow.com/questions/11696060/…
    – user6522773
    Jul 6, 2016 at 10:37
  • 7
    Well did you read the documentation for the method and try anything? The documentation includes examples already. It's important to be able to perform your own research rather than relying on other people giving you precise examples.
    – Jon Skeet
    Jul 6, 2016 at 10:38
  • Note that you can't verify if a user with birthday 10.12.1970 really meant 10.12. and not 12.10.
    – René Vogt
    Jul 6, 2016 at 10:41

2 Answers 2

2

This will return if it's a valid date or not:

String dob = System.Console.ReadLine();
DateTime dtResult;
bool IsValid = DateTime.TryParseExact(dob, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dtResult);

But in case of e.g. January 1st you can't detect a month/day swap. That will only result to false if the day > 12

1
  • This is exactly what I was looking for! Thanks a bunch! :) And thank you for bringing out the issue with this type of checking, I didn't even realise it. Will try something else to work around it later on.
    – Pepelius
    Jul 6, 2016 at 10:43
1

TryParseExact is your way :

DateTime dt = new DateTime();
bool success = DateTime.TryParseExact(dob, "dd.MM.yyyy", CultureInfo.CurrentCulture, 
                                        DateTimeStyles.AssumeLocal, out dt);

If dob is in correct format, isInCorrectFormat will be true and dt will hold the correct parsed DateTime object.

4
  • Thou Shalt Not make answers from Jon Skeet comments.
    – Uwe Keim
    Jul 6, 2016 at 10:38
  • 3
    @UweKeim there is no such rule in stackoverflow Jul 6, 2016 at 10:39
  • @M.kazemAkhgary Isn't that what one calls "respect" and/or "common sense"?
    – Uwe Keim
    Jul 6, 2016 at 10:40
  • @UweKeim i'd say you shall not answer within comments
    – fubo
    Jul 6, 2016 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.