如何正确地将时间和日期插入表格中? [英] How to correctly insert time and date into a table?
问题描述
我不想使用表格在表中插入时间和日期。
I't tyring to insert a time AND date into a table using a form.
<tr>
<td> Date (Please enter date format mm/dd/yyyy.):</td>
<td><INPUT TYPE="text" NAME="date_used" SIZE="10" VALIDATE="date"></td>
</tr>
<tr>
<td> Time (Please enter time format hh:mm tt):</td>
<td><INPUT TYPE="text" NAME="time_used" SIZE="10" VALIDATE="time" ></td>
</tr>
.....
<CFSET date_used = #DateFormat(CREATEODBCDATETIME(date_used), "mm/d/yyyy")#>
<CFSET time_used = #DATEFormat(CREATEODBCTIME(time_used),"h:mm tt")#>
但是当我将其插入表中时会出现错误
But when I insert it into the table I get a error
从字符串转换日期和/或时间时转换失败。
"Conversion failed when converting date and/or time from character string."
在将time_used插入表中的那一行上。
是否有更好的方法将特定的时间和日期插入表中?
Which is on the line im inserting the time_used into the table. Is there a better way to insert a specific time and date into a table?
推荐答案
首先是Dateformat和TimeFormat返回字符串,并且您需要一个datetime对象。实现该目标的方法是使用ParseDateTime。
First of all Dateformat and TimeFormat return strings and you want a datetime object. The way to get that is with ParseDateTime.
<cfset YourDateTime =
ParseDateTime(form.Date_used & ' ' & form.time_used, 'mask goes here')>
然后在查询中,使用cfqueryparam的cfsqltype属性分隔日期和时间。
Then in your query, use the cfsqltype attribute of cfqueryparam to separate the date and time.
insert into table (datefield, timefield)
values
(<cfqueryparam cfsqltype="cf_sql_date" value="#YourDateTime#">
, <cfqueryparam cfsqltype="cf_sql_time" value="#YourDateTime#">
)
您可以查找用于ParseDateTime()的掩码。
You can look up the mask to use for ParseDateTime().
这些都是基础知识。当然,您将不得不进行很多输入验证。您可能还想验证是否有必要让用户输入时间的秒部分。
Those are the basics. You will have to do a lot of input validation of course. You might also want to verify that it's necessary to make users enter the seconds portion of the time.
这篇关于如何正确地将时间和日期插入表格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!