-2

I am trying to parse a string to Datetime, but it is not working and giving an error:

"String was not recognized as a valid DateTime."

The string is perfect.

Here is the code:

string deliv = DeliveryDateTextBox.Text;

string[] delivday = deliv.Split('-');
int year, month, day;

int.TryParse(delivday[0], out day);
int.TryParse(delivday[1], out month);
int.TryParse(delivday[2], out year);

string dtt = day + "/" + month + "/" + year;

DateTime datet = DateTime.ParseExact(dtt, "dd/MM/yyyy", null);

jobcard.DeliveryDate = datet;

I debugged the code and it is giving {01-01-0001 12:00:00 AM} on datet.

Peter B
  • 22,460
  • 5
  • 32
  • 69
krazyhamad
  • 27
  • 6

3 Answers3

8

Besides the fact that you should be using new DateTime(year, month, day), or even DateTime.TryParseExact(deliv, "d-M-yyyy", .... ) on the original string...

Your call to DateTime.ParseExact() is failing because your input string has single-digit day and/or single-digit month, while your pattern dd/MM/yyyy demands double digits for both.

This can be fixed by using d/M/yyyy for the pattern, it will accept single and double digits. But please don't, see the first paragraph!

Peter B
  • 22,460
  • 5
  • 32
  • 69
1

You over complicated things in this code. Manually parsing the string to extract int values of day, month and year just to recombine them into a new string and parsing it makes no sense.

Simply try to parse the original string:

DateTime datet;
if(DateTime.TryParseExact(
   DeliveryDateTextBox.Text, 
   "dd-MM-yyyy", 
   CultureInfo.InvariantCulture, 
   DateTimeStyles.None, out datet))
{
    // string was successfully parsed to dateTime.
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Dear downvoter: Do you think this answer is wrong? If so, why? Please leave a comment when downvoting so that we can learn from it. – Zohar Peled Apr 11 '18 at 09:31
  • This is in my opinion the right solution, except that if this is user input I would use the format "d-M-yyyy" in order not to require the user to use leading zero in one digit days and months – Jeppe Alkærsig Apr 11 '18 at 11:45
  • @JeppeAlkærsig Thanks. however I think Christopher's answer is better - Simply don't use a textbox to get a date value from the user - use a datePicker instead... – Zohar Peled Apr 11 '18 at 11:47
1

There is no "perfect" DateTime String. How you can can/should represent a DateTime is dependent 100% on the CultureFormat of Windows, which can be totally different even within a langauge: For example en-gb and en-us disagree on what the decimal, thousand seperator are and in which order the date components should be listed.

Your whole code does not make a lot of sense. It seems you have some disjointed textboxes where the user inputs the day, month and year seperately. Then you parse them to int. Then you turn them into a string. Then you try to parse the string.

And at no point do you even check if the original user inputs are valid. That original parsing to Int might already fail. So you might try to parse 0/0/0 into a DateTime. To which your output is actually the perfect answer. And then there is stuff like anything before 1800 or so being literally not on the Gregorian calendar.

If you want the user to input a date, use a DatePicker element. Every GUI technology I know of has one. They give you DateTimes as return value. Do not try your custom workaround.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Christopher
  • 9,634
  • 2
  • 17
  • 31