-1

When I try this:

Convert.ToDateTime(newReg.dob)

I get the following error:

'Convert.ToDateTime(newReg.dob)' threw an exception of type 'System.FormatException'

However, when I do:

Convert.ToDateTime("3/3/2000")

It results in the following:

{3/3/2000 12:00:00 AM}

newReg.dob is a string "3/3/2000" - what is the difference? How do I get this to work?

What I'm seeing locally:

enter image description here

enter image description here

webdad3
  • 8,893
  • 30
  • 121
  • 223
  • 1
    Are you *sure* it contains that? Testing locally does not throw an error. – Rob Nov 21 '16 at 04:35
  • Put a line right above that to log it somewhere... you'll find it's not what you think it is. – Joel Coehoorn Nov 21 '16 at 04:36
  • 1
    You need a [mcve] that clearly demonstrates this issue otherwise we can't help. – Enigmativity Nov 21 '16 at 04:38
  • @Enigmativity - Will my screenshots help? – webdad3 Nov 21 '16 at 04:43
  • @Hogan - it was typo on my part. The same results occur even when I type Convert.ToDateTime("3/3/2000") – webdad3 Nov 21 '16 at 04:45
  • Is `dob` of type `string`? Assign it to a string and then convert that string. If that fails, then make that string be "3/3/2000". If it workes, then you have to see what's the difference between `dob` and that string. I guarantee you: there is something. BTW, making your method return a string containing "error" is a code smell to me. You should handle errors in a better way. – Andrew Nov 21 '16 at 04:45
  • what type is newReg.dob? – Hogan Nov 21 '16 at 04:48
  • @Hogan - string – webdad3 Nov 21 '16 at 04:51
  • Are both of your tests running in the same environment? Meaning, is it possible that the test is running under one, possibly the default CultureInfo, while the throwing code has the current thread's Culture set to something else? – Avner Shahar-Kashtan Nov 21 '16 at 04:53
  • @Andrew - I assigned newReg.dob to a string and then tried to Convert.ToDateTime(myDOBString) and received the same error - the code you are looking at is very early in dev. I'm going to change. – webdad3 Nov 21 '16 at 04:53
  • can you show the code context -- is it part of a closure or something like that? – Hogan Nov 21 '16 at 04:53
  • 5
    Also, is it possible that your `dob` string has invisible, non-printing characters, like a UTF-8 BOM? Compare the string's `Length` property with the character count you expect (8) – Avner Shahar-Kashtan Nov 21 '16 at 04:54
  • 1
    @AvnerShahar-Kashtan - That may be it. newReg.dob.Length = 13 and it only should contain 8 characters? How can I fix that? – webdad3 Nov 21 '16 at 04:56
  • Related : http://stackoverflow.com/q/2193012/5395773 – Venkat Nov 21 '16 at 05:04
  • @webdad3 - No, screenshots will only confirm what you're saying. You need code that we can run that replicates the error. – Enigmativity Nov 21 '16 at 06:11
  • @Enigmativity - I guess I didn't know what was happening myself and didn't know until after someone suggested the hidden characters. This one was gong to be difficult to replicate in code. I'll keep that in mind though for next time. – webdad3 Nov 21 '16 at 15:10
  • @webdad3 - The process of replicating this in code often leads to an answer along the way - or even simply a better question. – Enigmativity Nov 21 '16 at 23:06

1 Answers1

1

You better use DateTime.TryParseExact as below,

DateTime date;
if (DateTime.TryParseExact(newReg.dob.Trim(), "d/M/yyyy", CultureInfo.InvariantCulture,DateTimeStyles.None, out date))
{
   myPerson.personDOB =date;
}
else
{
   // Parse failed
}

By using above you can specify Culture invariant date time parse and also you can identify cases where this conversion failed, without exception.

in case of you have unwanted characters in the input string, you can use regex to get the content you want, or write simple code to filter unwanted content like below

string input ="$3/3/2000#$@#$ter ";
var outstring= new string(input.Where(c=>char.IsDigit(c)|| c=='/').ToArray()); //you get 3/3/2000 as result
// now you can pass this to above DateTime.TryParseExact method 
Damith
  • 62,401
  • 13
  • 102
  • 153
  • At best, this would be a workaround for this exact string. At worst, as is apparently the case, it won't work because the real issue is that the string contains invisible characters. – Avner Shahar-Kashtan Nov 21 '16 at 05:00
  • @AvnerShahar-Kashtan I haven't see the discussion on comments section until I post this answer. updated my answer. Thanks – Damith Nov 21 '16 at 05:14
  • After I removed the extra characters from my string as you suggested above all options started to work! – webdad3 Nov 21 '16 at 05:16