0

我有以下代码:

Dim dtDoM As Nullable(Of DateTime)
If (txtMarriageDate.Text.Trim = "") Then
            dtDoM = Nothing
        Else

            dtDoM = DateTime.ParseExact(txtMarriageDate.Text.Trim + " 00:00:00", "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
            'dtDoM = Convert.ToDateTime(txtMarriageDate.Text)
        End If

在注释掉的部分,我得到了 FormatException 'String not Recognized as datetime',而在新代码中,我得到了“System.FormatException = {“日历 System.Globalization.GregorianCalendar 不支持字符串表示的 DateTime。” }"

编辑:回答。不敢相信我忽略了这一点。

4

3 回答 3

1

您的日期(01/25/1955)是MM/dd/yyyy格式,您dd/MM/yyyy在格式字符串中使用。

尝试使用这个:"MM/dd/yyyy hh:mm:ss"

于 2012-04-17T17:24:05.353 回答
1

我认为您的格式字符串错误。"MM/dd/yyyy hh:mm:ss"从你的例子来看,你想要。

于 2012-04-17T17:24:42.507 回答
1

你把日期和月份颠倒了。这将起作用:

dtDoM = DateTime.ParseExact(txtMarriageDate.Text.Trim + " 00:00:00", "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
于 2012-04-17T17:30:58.890 回答