问题描述
我试图插入到数据库 - 关于事件的各种详细信息。在asp.net文本框使用日历扩展(那么一点点日历弹出,并用正确格式填写日期文本框)。在我的Access数据库EVENTDATE字段类型日期/时间。我需要将文本/字符串转换为日期/时间格式
到目前为止,我已经试过这样:
VB:
Here is my clientside code just for reference:
When I try to fill out the form, I receive this error:
My Two questions are:
- What is wrong with the code above, and how do I successfully use the DateTimeFormatInfo class to convert String to Date/Time?
- On a side note, the Calendar Extender inputs the date into the textbox in American Time format (MM/DD/YYYY), how do I change this to British (DD/MM/YYYY) format (I couldn't see an obvious property in the properties dialog to do this?)
Thanks in advance for your answers!
Adam
EDIT: Updated code below:
I'd recommend you use DateTime.ParseExact
static method, especially this oveload:
DateTime.ParseExact(textBox.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture
)
This will parse the text you have by the concrete format you specify ("dd/MM/yyyy"
currently, case is important since mm
is minutes as opposed to MM
being months). Use of CultureInfo.InvariantCulture
guarantees that date separators will be retrieved from the format string (the second parameter). I have noticed that if current culture is used, it overrides some aspects of the format string you pass to ParseExact
.
A note on CultureInfo
Invariant culture is good also for the reason that your local dev environment may have different regional information setup than the deployment environment. Usually, .NET uses the current culture in all .ToString
calls and implicit formatting or parsing. When forcing a format and culture invariance explicitly, you are less prone to problems you cannot reproduce locally but exist on the production application.
A note on date/time formats
With exact parsing, the datetime format is expected to strictly match the format of the input. You should then take into consideration the following examples:
dd
matches two-digit days only. So"dd/MM/yyyy"
it will match"01/01/2013"
, but will fail for"1/1/2013"
because it expects the exact number of digits for the day part. If you do not want leading zeros use:d/M/yyyy
instead. Single letter means one digit for days less than10
and two digits for the others.MM
matches two-digit month, so all that applies todd
vs.d
is the same for months.yyyy
expects the year to be in 4 digits. If you use two-digit year, useyy
instead.
A note on some ADO.NET providers
As it turns out to be the case with MS Access, the correctly parsed date-time object is not sufficient to make the query work. Currently, the following code
cmd.Parameters.AddWithValue(...)
is used to add parameters to the query. However, this approach omits passing information to the ADO.NET db provider that tells what database type to use for the parameter. I have read on some forums that MS Access/OleDb is not capable to resolve the correct type in all cases. Therefore I recommend the following approach:
Dim prm as OleDbParameter = _
New OleDbParameter("@dateTimeParameterName", OleDbType.DateTime)
prm.Value = value 'value is an instance of `System.DateTime` parsed
'from the input
cmd.Parameters.Add(prm)
The above code allows to specify the parameter database type explicitly, so the OleDb driver is now capable of correctly passing the DateTime
object to the MS Access database.
这篇关于转换ASP.NET文本框控件的.text内容,日期/时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!