该字符串未被识别为有效的日期时间

时间:2022-10-12 10:37:31

objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))

following error is coming

以下错误即将来临

The string was not recognized as a valid datetime .There is an unknown word starting at 0.

字符串未被识别为有效日期时间。从0开始有一个未知单词。

What should i do to save this datetime, please help

我该怎么做才能保存这个日期时间,请帮忙

3 个解决方案

#1


You can't format normal text using datetime formats.

您无法使用日期时间格式格式化普通文本。

Try

C#

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);

VB.NET

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)

This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.

这假设dateFileDate是DateTime类型,并且txtdate.text采用上述格式。

#2


If your program is used by a international crowd, read on :)

如果您的程序被国际人群使用,请继续阅读:)

ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control? Im saying this based on personal experience. Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)

来自不同文化的ppl将以不同的格式写日期,所以如果你总是要解析可能变得粘滞的字符串。考虑使用压延控制?我根据个人经验说这个。同时找出你当前失败的原因,我会做一个DateTime.Now.ToString()并将其与文本框中的whats进行比较,这样你就可以看到输入错误了什么(在调试过程中,以帮助追踪问题)

#3


Try hh:nn instead of hh:mm

试试hh:nn而不是hh:mm

I believe mm is Months in two digit format and nn is minutes in two digit format.

我相信mm是两位数格式的月份,而nn是两位数格式的分钟。

#1


You can't format normal text using datetime formats.

您无法使用日期时间格式格式化普通文本。

Try

C#

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);

VB.NET

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)

This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.

这假设dateFileDate是DateTime类型,并且txtdate.text采用上述格式。

#2


If your program is used by a international crowd, read on :)

如果您的程序被国际人群使用,请继续阅读:)

ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control? Im saying this based on personal experience. Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)

来自不同文化的ppl将以不同的格式写日期,所以如果你总是要解析可能变得粘滞的字符串。考虑使用压延控制?我根据个人经验说这个。同时找出你当前失败的原因,我会做一个DateTime.Now.ToString()并将其与文本框中的whats进行比较,这样你就可以看到输入错误了什么(在调试过程中,以帮助追踪问题)

#3


Try hh:nn instead of hh:mm

试试hh:nn而不是hh:mm

I believe mm is Months in two digit format and nn is minutes in two digit format.

我相信mm是两位数格式的月份,而nn是两位数格式的分钟。