0

We need to convert the system date to a appropriate format as per the specific culture. We observe a strange behavior where-in we're not able to convert the DateTime for March month alone for Italy culture.

    Try

        Dim dt As String = "01-Mar-2016"
        Dim culture As New CultureInfo("it-IT")

        MsgBox(Convert.ToDateTime(dt, culture).ToShortDateString)

    Catch ex As Exception

        MsgBox(ex.ToString)

    End Try

The above code works for Jan, Feb, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov & Dec, but not only for Mar. Is this any existing bug within Windows..?

PS: The above code throws "String was not recognized as a valid DateTime" exception, but works fine when we change the date as "01-March-2016".

user3138788
  • 53
  • 1
  • 1
  • 7
  • It works fine when parsing: `DateTime.ParseExact(dt, "dd-MMM-yyyy", culture).ToShortDateString()` – Ňɏssa Pøngjǣrdenlarp Mar 02 '16 at 18:05
  • Hi @Plutonix, thanks for the information. I'll have it checked. But is there any reason why the above logic should fail only for Mar for Italy..? – user3138788 Mar 02 '16 at 18:12
  • Strictly speaking that does not appear to be a valid format for Italy. They have `d-MMM-yy` and `d MMMM yyyy` but that is not quite the same, so parsing is more appropriate than converting. [NetFiddle of valid formats](https://dotnetfiddle.net/WImdZ7) – Ňɏssa Pøngjǣrdenlarp Mar 02 '16 at 18:17

2 Answers2

0

I got the error using .NET 4.0, but it works with 4.5. And in 4.0 it fails for both DateTime.Parse and Convert.ToDateTime. I've read that the latter calls the former. That seems to confirm it. So apparently they realized the issue and fixed it.

Why only Mar? It's the only one that's an abbreviation for both a month and a day (Tuesday) in Italian.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
-1

Easy and quick solution...

fecha_temp = CDate(fecha_ant.Replace(" Mar ", " March "))
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
JMB
  • 1