Henry
Henry

Reputation: 641

ASP.NET vb.net convert calendar date format in Month/Day/Year to Date object with Day/Month/Year

I have a calendar control:
<asp:Calendar ID="cldDepartDate" runat="server"></asp:Calendar>

And for example, if I chose 14th of March 2019, using the code below to display:
lblTest.Text = cldDepartDate.SelectedDate.ToString()

I got "3/14/2019 12:00:00 AM"

And now I want to convert it to "14/3/2019 12:00:00 AM" and store it into a Date object.

So far I have tried:

Dim oDate As Date = Date.ParseExact(cldDepartDate.SelectedDate.ToString("dd/MM/yyyy"), "MM/dd/yyyy", CultureInfo.InvariantCulture)

But it gives me this error:
enter image description here

Upvotes: 0

Views: 1584

Answers (1)

Mary
Mary

Reputation: 15091

    Dim dateString = "3/14/2019 12:00:00 AM"
    Dim oDate As Date = Date.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
    Debug.Print(oDate.ToString("dd/M/yyyy hh:mm:ss tt"))

Using the standard DateTime formats.

https://learn.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo?view=netframework-4.7.2#Formatting_dates_times

Upvotes: 1

Related Questions