3

The following line gives me a "String was not recognized by a valid DateTime" error:

DateTime.ParseExact("4/6/2016", "dd/MM/yyyy", Nothing)

I got this "working" example on stackoverflow, but it doesn't work for me: Parse Exact Sample and I can't figure out why.

EDIT (reedit, I mistakenly typed the last two attempts wrong): Still no good. This is what I've found after trying all the suggestions submitted so far (thanks all). Some more info, the date string is coming from a textbox control.

Dim xxx As String = "4/6/2016"  'OR  = "04/06/2016" as necessary for the sample    

This does not work:

DateTime.ParseExact(xxx, "M/d/yyyy", CultureInfo.InvariantCulture)

This does not work:

DateTime.ParseExact(xxx, "MM/dd/yyyy", CultureInfo.InvariantCulture)

After, I tried something simpler with just DateTime.Parse:

DateTime.ParseExact(xxx)

I typed this out by hand. I did NOT use values from a textbox control and it DID work:

DateTime.Parse("4/6/‎2016‎")

So frustrating

Community
  • 1
  • 1
Carlos Mendieta
  • 860
  • 16
  • 41
  • 2
    Maybe if you try this way: `DateTime.ParseExact("04/06/2016", "dd/MM/yyyy", Nothing)` ..... – Hackerman Apr 12 '16 at 17:17
  • 2
    conclusion: the value in your textbox is not what you typed out by hand - use the debugger to see exactly what value is being used. Are you sure it is M/d/yyyy and not d/M/yyyy for example? – Matt Wilko Apr 12 '16 at 17:44

2 Answers2

7

Your format says you'll have two digits for the day and the month. Your actual values only have one digit.

You have two options:

  • Follow the format you've specified, e.g. 04/06/2016
  • Change the format to match the value, e.g. d/M/yyyy

Additionally - and separately - you're passing in Nothing as the format provider, which means the current culture will be used. Your format uses / which is the culture-specific date separator. If your current culture doesn't use / as its date separator, that's another problem. If you've got a fixed format, you probably want to specify CultureInfo.InvariantCulture as the format specifier instead.

Here's a short but complete program demonstrating it working:

Option Strict On

Imports System
Imports System.Globalization

Public Class Test

    Shared Sub Main()
        Dim xxx As String = "4/6/2016"
        Dim result as DateTime = DateTime.ParseExact(xxx, "M/d/yyyy", CultureInfo.InvariantCulture)
        Console.WriteLine(result)
    End Sub

End Class
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @CarlosMendieta: `DateTime.ParseExact(xxx, "M/d/yyyy", CultureInfo.InvariantCulture)` works fine for me. What *exactly* do you mean by "doesn't work"? Ideally, show a [mcve] demonstrating the problem. It sounds like maybe your text box doesn't actually have the string you think it does... – Jon Skeet Apr 12 '16 at 17:44
  • 1
    @CarlosMendieta: I've now included a short but complete program simply copying and pasting bits of code that you claimed don't work, and putting them in a `Main` method. It works fine. It sounds like what isn't working for you *isn't* what you've shown. – Jon Skeet Apr 12 '16 at 17:46
1

When you use dd or MM it expects a two digit number, so you would need to use "04/06/2016" or else replace them with their single digit versions of d and M.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431