How to ask C# date of birth in a certain format with limitations? [closed]
up vote
-3
down vote
favorite
I'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
c# date dob
closed as off-topic by Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo Nov 8 at 22:24
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
up vote
-3
down vote
favorite
I'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
c# date dob
closed as off-topic by Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo Nov 8 at 22:24
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
I imagine you can just accept the input as a string and useDateTime.TryParseExact()to specify the format when converting that string to aDateTimevalue. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing aDateTimefrom astring.
– David
Nov 8 at 19:40
1
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
I can get it to ask but it won't specify...what is "it"`? A Console program? If not, try a DateTimePicker
– Disaffected 1070452
Nov 8 at 19:42
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45
|
show 1 more comment
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
c# date dob
I'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
c# date dob
c# date dob
edited Nov 8 at 19:53
Michał Turczyn
12.9k132039
12.9k132039
asked Nov 8 at 19:38
duniel
111
111
closed as off-topic by Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo Nov 8 at 22:24
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo Nov 8 at 22:24
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Juan Castillo, Ashkan Mobayen Khiabani, user6910411, Amy, Richardissimo
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
I imagine you can just accept the input as a string and useDateTime.TryParseExact()to specify the format when converting that string to aDateTimevalue. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing aDateTimefrom astring.
– David
Nov 8 at 19:40
1
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
I can get it to ask but it won't specify...what is "it"`? A Console program? If not, try a DateTimePicker
– Disaffected 1070452
Nov 8 at 19:42
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45
|
show 1 more comment
2
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
I imagine you can just accept the input as a string and useDateTime.TryParseExact()to specify the format when converting that string to aDateTimevalue. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing aDateTimefrom astring.
– David
Nov 8 at 19:40
1
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
I can get it to ask but it won't specify...what is "it"`? A Console program? If not, try a DateTimePicker
– Disaffected 1070452
Nov 8 at 19:42
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45
2
2
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
I imagine you can just accept the input as a string and use
DateTime.TryParseExact() to specify the format when converting that string to a DateTime value. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing a DateTime from a string.– David
Nov 8 at 19:40
I imagine you can just accept the input as a string and use
DateTime.TryParseExact() to specify the format when converting that string to a DateTime value. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing a DateTime from a string.– David
Nov 8 at 19:40
1
1
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
I can get it to ask but it won't specify... what is "it"`? A Console program? If not, try a DateTimePicker– Disaffected 1070452
Nov 8 at 19:42
I can get it to ask but it won't specify... what is "it"`? A Console program? If not, try a DateTimePicker– Disaffected 1070452
Nov 8 at 19:42
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
add a comment |
up vote
1
down vote
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
add a comment |
up vote
1
down vote
up vote
1
down vote
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");
answered Nov 8 at 19:48
Michał Turczyn
12.9k132039
12.9k132039
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
add a comment |
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
@Flydog57 Well, that was just example culture matching OP's date :)
– Michał Turczyn
Nov 8 at 20:53
add a comment |
2
Please edit your question and share your code with us, and we would be happy to help.
– Amy
Nov 8 at 19:39
I imagine you can just accept the input as a string and use
DateTime.TryParseExact()to specify the format when converting that string to aDateTimevalue. Note that the "limitations" you've defined do not accurately express a date value. For example, "31.02.2018" is not a valid date. Don't re-invent the wheel with dates, use the tools available to you. In this case, parsing aDateTimefrom astring.– David
Nov 8 at 19:40
1
tried anything yet?
– Ashkan Mobayen Khiabani
Nov 8 at 19:41
I can get it to ask but it won't specify...what is "it"`? A Console program? If not, try a DateTimePicker– Disaffected 1070452
Nov 8 at 19:42
Are you expecting many users coming from the future?
– Zohar Peled
Nov 8 at 19:45