Javascript 如何让引导DateTimePicker显示正确的年份?
我维护的网站功能之一是创建/更新即将到来的活动。用户需要能够选择事件的日期和时间,但我发现,如果选择的日期是在一年的最后一周,并且该周中有几天是在下一年,那么该年将自动设置为下一年 例如,如果选择2019年12月29日,则插入数据库的日期为2020年12月29日。 此外,如果我打开一个包含事件数据的模块,它将显示日期为2021年12月29日,即使数据库记录为2020年 由于2019年12月28日是星期六,与2020年的几天不在同一周,因此将作为2019年12月28日插入数据库 下面是事件模块的一些ColdFusion代码:Javascript 如何让引导DateTimePicker显示正确的年份?,javascript,html,twitter-bootstrap,coldfusion,Javascript,Html,Twitter Bootstrap,Coldfusion,我维护的网站功能之一是创建/更新即将到来的活动。用户需要能够选择事件的日期和时间,但我发现,如果选择的日期是在一年的最后一周,并且该周中有几天是在下一年,那么该年将自动设置为下一年 例如,如果选择2019年12月29日,则插入数据库的日期为2020年12月29日。 此外,如果我打开一个包含事件数据的模块,它将显示日期为2021年12月29日,即使数据库记录为2020年 由于2019年12月28日是星期六,与2020年的几天不在同一周,因此将作为2019年12月28日插入数据库 下面是事件模块的一
<div class="form-group">
<cfdump var="#event.event_date_time#" expand="yes"></cfdump>
<label>Event Date and Time:</label>
<div class='input-group date' id='updateEventDateTime'>
<input required type='text' name="updateEventDateTime" class="form-control"
placeholder="Select event date and time"
value="#DateTimeFormat(event.event_date_time,'MM/dd/YYYY H:nn tt')#"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<cfdump var="#event.event_date_time#" expand="yes"></cfdump>
</div>
活动日期和时间:
以下是DateTimePicker的javascript:
<script>
$(document).ready(function(e) {
// Create the Bootstrap Datetimepicker for the event
// date and time
$('#updateEventDateTime').datetimepicker({
format: 'MM/DD/YYYY H:mm A',
sideBySide: true
});
});
</script>
$(文档).ready(函数(e){
//为事件创建引导Datetimepicker
//日期和时间
$('#updateEventDateTime')。日期时间选择器({
格式:'MM/DD/YYYY H:MM A',
旁边:对
});
});
下面是上述ColdFusion代码输出的屏幕截图:
日期时间中的年份错误:
我该怎么做才能解决这个问题?在过去的两年里,我维护这个网站并不是一个问题,虽然现在对用户来说显然不是问题,但它将在11月/12月出现,我宁愿现在就修复它
编辑:有两个主要操作都需要修复。用户需要能够添加事件和更新事件。上面的ColdFusion代码位于updateEvent模块中,addEvent模块的部分代码如下:
<div class="form-group">
<label>Event Date and Time:</label>
<div class='input-group date' id='addEventDateTime'>
<input required type='text' name="addEventDateTime" class="form-control"
placeholder="Select event date and time" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
活动日期和时间:
此模块中的javascript代码包含以下内容:
<script>
$(document).ready(function(e) {
// Create the Bootstrap Datetimepicker for the event
// date and time
$('#addEventDateTime').datetimepicker({
format: 'MM/DD/YYYY H:mm A',
sideBySide: true
});
$(文档).ready(函数(e){
//为事件创建引导Datetimepicker
//日期和时间
$(“#addEventDateTime”).datetimepicker({
格式:'MM/DD/YYYY H:MM A',
旁边:对
});
我解决了我的问题。当按下“保存”按钮时,数据被放入一个结构中,该结构被传递到以下函数中:
<cffunction name="addEvent" access="public" returntype="any">
<cfargument name="eventStruct" type="struct" required="yes">
<!--- Verify the structure is formated correctly --->
<!--- Checks if all the required elements of the struct are there --->
<!--- Verify the date object is correct --->
<cfif NOT isDate(ARGUMENTS.eventStruct.eventDateTime)>
<cfset errorCode = "INVALID_VARIABLE_TYPE">
<cflog file="chapter-reports" type="error"
text="#errorCode#: variable must be a coldfusion date object">
<cfreturn errorCode />
</cfif>
<!--- Format the date --->
<cfset ARGUMENTS.eventStruct.eventDateTime = DateTimeFormat(ARGUMENTS.eventStruct.eventDateTime,"yyyy-MM-dd HH:nn:ss")>
<!--- Insert the event into the table --->
<cftry>
<cfquery datasource="#VARIABLES.datasource#" name="insertEvent" result="newEvent">
INSERT INTO #VARIABLES.tablePrefix#events
(/*column names*/,event_date_time,/*more column names*/)
VALUES(
/*data*/,
'#ARGUMENTS.eventStruct.eventDateTime#',
/*more data*/
)
</cfquery>
<!--- Send an error message if there is a database error --->
</cftry>
<cfreturn newEvent.generated_key />
插入到#变量中。tablePrefix#事件
(/*列名*/,事件日期时间,/*更多列名*/)
价值观(
/*数据*/,
“#ARGUMENTS.eventStruct.eventDateTime#”,
/*更多数据*/
)
最初,它会将日期格式化为“YYYY”,但在将其更改为“YYYY”之后,它可以按预期工作。您在这里谈论的是多个不同的事情:您使用的是引导模板中的Javascript、ColdFusion变量和数据库。所有这三个都会影响您看到的最终日期,并且您看到的不是日期对象(就像数据库最终应该需要的那样),而是日期对象的字符串表示形式 当你看一个日期时,它可能看起来像是2019年3月27日12:00:00 pm或2019年3月27日12:00:00 pm或2019年3月27日12:00:00.000之类。当数据库或编程语言将该字符串视为日期对象时,它看起来像是
123465789.123465798
,这通常是一个特定的变量从系统组件使用的历元开始的秒数。epoch
是一个完全不同的主题
由于一个长的十进制数并不能作为一个日期真正让人可读,因此您的语言必须执行掩蔽来在两种表示之间来回转换
您需要记住使用哪段代码进行屏蔽。通常,屏蔽用于显示,但在插入字符串之前,您可能还需要将其转换为数据库上的日期可读对象。因此,您需要检查Javascript是否对表单中的值执行任何操作,以及您的ColdFusion没有对该Javascript字符串做任何操作,并且您的数据库也没有进一步更改该ColdFusion字符串
我不确定Bootstrap的datapicker
使用什么来解释日期字符串,或者它是否最终会向ColdFusion发送一个奇数字符串,但我知道CF11根据您使用的函数使用不同的掩码。DateFormat()
不关心您使用的是哪种情况,但DateTimeFormat()
确实如此。它依赖于其底层JavaSimpleDataFormat
的屏蔽定义,该定义将y
和y
解释为不同的屏蔽,表示不同的东西
最大的痛苦是,正如你所看到的,它在一年结束时只有几天是可见的,因为一年从来没有精确地由52周组成。因此,我们有另一种类型的日期称为WeekYear
,或SimpleDateFormat
掩蔽中的Y
。它基本上是基于周的年份(W
或W
)。因此,在一年的开始和结束的几天内,日历年将不等于周年。对于一年中的其他358天左右,它们将是相同的。由于新年和除夕通常是非工作假期,因此很容易被忽略。因此,本质上,您的代码在过去几年中没有正常工作;y你只是没有理由注意到而已。很有趣,对吧
有关简单示例,请参见:
如果您注意到,dateFormat()
和timeFormat()
都会屏蔽超出“技术”要求的内容。您可以让timeFormat()
对字符串的date
部分应用屏蔽。它们不应该以这种方式工作
正如我在多个地方说过的,日期处理(在几乎任何语言中)都是p