Validating date c

29-May-2019 09:35

Here is what I know I can use, but there are potentially thousands of dates to validate, and I know this code is inefficient if there are lots of dates that won't convert...

private Date Time Validate Date(string date) Thanks, John John, You can use the code you have, or, if you have access to the .

The first function inputyear() is used to get the user input. Note: that there is no input checking or error handling is performed to keep things simple.) The next function determinedaycode() is used to get the day number of the first day in that year, so we can print the date on the correct position.

(So it is only used for output purposes.) The next function determineleapyear() is used to determine if input of the user is a leap year.

NET 2.0 beta, you can use the static Try Parse or Try Parse Exact method on the Date Time structure. NET/C# MVP] - mv*@spam.guard."John Livermore" In C# what is the best way to validate that a particular string entered by a user will actually convert to a date w/o using a try catch block or writing code to explicitly parse the string?

Another option would be to call the static Is Date method on the Interaction class in the Microsoft. It is a little more lenient than the Try Parse and the Try Parse Exact methods. Here is what I know I can use, but there are potentially thousands of dates to validate, and I know this code is inefficient if there are lots of dates that won't convert...

Your point of don't fix it unless it is broken is well taken, but I feel this will be an issue for us and should be addressed through some other means.

We are exploring the use of regular expressions to validate the input before we attempt to cast it as a date.

Further, I wouldn't be surprised in the least if the regex version of what you come up with is quite a bit slower than the try/catch version.Here is what I know I can use, but there are potentially thousands of dates to validate, and I know this code is inefficient if there are lots of dates that won't convert... Do you have specific performance requirements that aren't being met? Rather you should use other means to detect the condition.Try...catch is for errors you don't know are going to occur (bugs) to keep your application behaving nicely.I would like to check for the validity of a date (such as a February date for leap year) in C#. What I want is to validate the date for any month, day and year; furthermore, the day is to be validated against a particular month and year (not just within 1 - 31). Wellens: You deleted my identical response because you said "Try Parse Exact" was already suggested. Today; Date Time new Date; bool result = Date Time. To Short Date String(), out new Date); // true means correct date bool result1 = Date Time.I know in VB, you can use the Is Date function to do this. For example, 2/29/2008 is OK, but 2/29/2007 is not (because 2007 is a leap year). Thanks in advance, sg2000 Hi sg2000, I just faced with the problem and luckily saw your post, thank you very much for sharing your acquisition with people. This is incorrect - "Try Parse Exact" is not the same as "Try Parse". Min Value) Is Date("0") == false The long form of the Try Parse method is required so that we can specify Date Time Styles. If you don't do this then "0:0" is parsed as being midnight of the current date and would thus be accepted as a valid date.

Further, I wouldn't be surprised in the least if the regex version of what you come up with is quite a bit slower than the try/catch version.Here is what I know I can use, but there are potentially thousands of dates to validate, and I know this code is inefficient if there are lots of dates that won't convert... Do you have specific performance requirements that aren't being met? Rather you should use other means to detect the condition.Try...catch is for errors you don't know are going to occur (bugs) to keep your application behaving nicely.I would like to check for the validity of a date (such as a February date for leap year) in C#. What I want is to validate the date for any month, day and year; furthermore, the day is to be validated against a particular month and year (not just within 1 - 31). Wellens: You deleted my identical response because you said "Try Parse Exact" was already suggested. Today; Date Time new Date; bool result = Date Time. To Short Date String(), out new Date); // true means correct date bool result1 = Date Time.I know in VB, you can use the Is Date function to do this. For example, 2/29/2008 is OK, but 2/29/2007 is not (because 2007 is a leap year). Thanks in advance, sg2000 Hi sg2000, I just faced with the problem and luckily saw your post, thank you very much for sharing your acquisition with people. This is incorrect - "Try Parse Exact" is not the same as "Try Parse". Min Value) Is Date("0") == false The long form of the Try Parse method is required so that we can specify Date Time Styles. If you don't do this then "0:0" is parsed as being midnight of the current date and would thus be accepted as a valid date. In the C tutorial “How to use Time and Date in C” some people asked questions in the comment section about determining dates and day of the week.