I need to perform a check based on a string value whether its a date or decimal but date parse always return true for decimal.
string val = "3.5";
DateTime oDate = DateTime.Parse(val);
It returns a valid date 3/5/2019
.
How to validate string to know its a valid date when date format is not known?
C# includes DateTime struct to work with dates and times. To work with date and time in C#, create an object of the DateTime struct using the new keyword. The following creates a DateTime object with the default value. The default and the lowest value of a DateTime object is January 1, 0001 00:00:00 (midnight).
The Parse method tries to convert the string representation of a date and time value to its DateTime equivalent. It tries to parse the input string completely without throwing a FormatException exception.
Use the DateTime. TryParseExact method in C# for Date Format validation. They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.
The DateTime.Parse overloads return a DateTime value whose Kind property includes time zone information. It can indicate that the time is: Coordinated Universal Time (System.DateTimeKind.Utc). The time in the local time zone (System.DateTimeKind.Local).
The string to parse. A string with a time but no date component. The method assumes the current date unless you call the Parse(String, IFormatProvider, DateTimeStyles) overload and include DateTimeStyles.NoCurrentDateDefault in the styles argument, in which case the method assumes a date of January 1, 0001.
To parse a specific date and time format across different locales, use one of the overloads of the DateTime.ParseExact method and provide a format specifier. All overloads of the Parse method are culture-sensitive unless the string to be parsed (which is represented by s in the following table) conforms to the ISO 8601 pattern.
To prevent the difficulties in parsing data and time strings that are associated with changes in cultural data, you can parse date and time strings by using the invariant culture, or you can call the ParseExact or TryParseExact method and specify the exact format of the string to be parsed.
"How to validate string to know its a valid date?"
The issue is that "3.5"
is considered a valid date (and also a decimal).
If you want the decimal type to always "win" (i.e. you don't want isDate
and isDecimal
to both be true
), include a decimal check in your validation.
One way to do it is to use the TryParse
methods (which return a bool
if a string can be parsed to the type, and which sets an out
parameter to the converted value) to determine if the string can be converted to a type, for example:
string val = "3.5";
// temp variables to hold parsed values
DateTime tmpDate;
decimal tmpDec;
int tmpInt;
var isDecimal = decimal.TryParse(val, out tmpDec);
var isInteger = int.TryParse(val, out tmpInt);
// When checking if it's a DateTime, make sure it's not also a decimal
var isDate = !isDecimal && DateTime.TryParse(val, out tmpDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With