ASP.NET vb.net将日/月/年中的日历日期格式转换为日/月/年的日期对象

问题描述 投票:0回答:1

我有一个日历控件: <asp:Calendar ID="cldDepartDate" runat="server"></asp:Calendar>

例如,如果我选择2019年3月14日,使用下面的代码显示: lblTest.Text = cldDepartDate.SelectedDate.ToString() 我得到了“3/14/2019 12:00:00 AM”

现在我想将其转换为“14/3/2019 12:00:00 AM”并将其存储到Date对象中。

到目前为止,我尝试过:

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

但它给了我这个错误: enter image description here

asp.net vb.net datetime calendar string-conversion
1个回答
1
投票
    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"))

使用标准DateTime格式。

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

© www.soinside.com 2019 - 2024. All rights reserved.