Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal values recognized as DateTime instead of returning false from DateTime.Parse

Tags:

c#

.net

datetime

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?

like image 239
Munawar Avatar asked Dec 18 '19 13:12

Munawar


People also ask

Is date function in C#?

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).

What does DateTime parse do?

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.

How check DateTime is valid or not in C#?

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.

What is the use of datetime parse?

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).

How do I parse a string with no current date?

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.

How to parse a specific date and time format across locales?

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.

How to parse date and time strings with invariant culture?

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.


1 Answers

"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);
like image 75
Rufus L Avatar answered Oct 02 '22 12:10

Rufus L