Coldfusion datepicker dd / mm / yyyy但MySQL保持格式为yyyy / mm / dd [英] Coldfusion datepicker dd/mm/yyyy but MySQL stay with format yyyy/mm/dd

查看:164
本文介绍了Coldfusion datepicker dd / mm / yyyy但MySQL保持格式为yyyy / mm / dd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Datepicker 是用户可以轻松选择日期填写表单的选项。在ColdFusion中,有一个填充表单需要使用 datepicker ,并且在用户选择日期并填写表单后,格式为yyyy / mm / dd,默认格式为MySql读。如果我更改为dd / mm / yyyy并单击保存到MySql将会出错,因为从我所知的MySql默认格式是yyyy / mm / dd。

 < input type =textname =Date_joinedsize =autostyle =border:0pxrequired = 是 > 

这是弹出日期选择器的功能:

 < a href =javascript:showCal('Calendar1')> 

这是datepicker的徽标:

 < img align =rightsrc =calendar_menu.gifalt =选择日期border =0>< / a> 

是否有解决方案供用户选择日期和输入文字将显示dateformat dd / mm / yyyy但仍然可以保存到MySql中,而不会出错。 DD / MM / YYYY,因此数据将被记录


如果您想要保存一个字符串的数字并用符号表示代表 显示在屏幕上的格式的日期,然后将数据库列作为CHAR(10)并完成它。但是,如果您想对它进行计算,请按照它汇总数据,然后将它保存为日期类型。不要担心数据库UI如何向您呈现日期值。也许它与你希望在HTML页面上显示的方式不同,这并不重要。重要的是,作为日期对象,您可以轻松使用并显示该值,无论您喜欢。


据我所知,MySQL只接受数据类型date,格式为YYYY / MM / DD。

不行。


https://dev.mysql.com/doc/refman/5.7/en/datetime.html
DATE类型用于包含日期部分但没有时间部分的值。
MySQL 以'YYYY-MM-DD'格式检索和显示 DATE值。 >支持的范围是'1000-01-01'到'9999-12-31'。


请参阅检索并显示(强调我的)?它只是一个以00:00:00作为时间部分的日期对象。

因此,无论您的表单字段接受日期的字符串表示形式,您都需要将其转换为适当的日期对象。根据Dan的建议,您可以轻松使用 parseDateTime()来完成此操作。



#writeOutput(parseDateTime(now()))#会输出 {ts' 2018-03-14 15:29:19'}



如果您的表单字段包含表示日期的有效字符串(例如 2018-03-14 ):

#writeOutput(parseDateTime(form.myDateField)) #会输出 {ts'2018-03-14 00:00:00'}



所以这个值将被保存为日期对象,没有时间部分。当您稍后读取保存的值时,只需使用 dateFormat()以任意格式显示它即可。

Datepicker is an option for user easy to pick the date to fill the form. In coldfusion, there is a fill form that need to use datepicker and after user selected the date and fill the form with format yyyy/mm/dd by default format which is MySql can read. If i change into dd/mm/yyyy and click save into MySql will get error because from what i know default format for MySql is yyyy/mm/dd.

<input type="text" name="Date_joined" size="auto" style="border:0px"required="yes">

This is the function to popup datepicker :

<a href="javascript:showCal('Calendar1')">

This is logo for datepicker :

<img align="right" src="calendar_menu.gif" alt="Select a date" border="0"></a>

Is there any solution for user pick a date and input text will display dateformat dd/mm/yyyy but still can save into MySql without error.

解决方案

How to make MySQL accept a user input in format DD/MM/YYYY so the data will be recorded

If you want to save a string of numbers and dashes that represents a date in the format you want it displayed on a screen, then just make your database column a CHAR(10) and be done with it.

But, if you want to do calculations against it, aggregate data by it, DO THINGS with it, then save it as a date type. Don't worry about how your database UI represents that date value to you. Maybe it's different from how you want it shown on an HTML page, it doesn't matter. What matters is that as a date object, you can easily use and display that value however you like.

From what I know, MySQL will only accept datatype date with format YYYY/MM/DD.

Not how that works.

https://dev.mysql.com/doc/refman/5.7/en/datetime.html The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. > The supported range is '1000-01-01' to '9999-12-31'.

See that "retrieves and displays" (emphasis mine)? It's just a date object with "00:00:00" as the time portion.

So however your form field accepts the string representation of the date, you need to convert it to a proper date object. Per Dan's suggestion, you can easily use parseDateTime() to accomplish this.

#writeOutput( parseDateTime( now() ) )# will output {ts '2018-03-14 15:29:19'}.

If your form field contains a valid string that represents a date (e.g. 2018-03-14):

#writeOutput( parseDateTime( form.myDateField ) )# will output {ts '2018-03-14 00:00:00'}.

So the value will be saved as a date object, without the time portion. When you read the saved value later, just use dateFormat() to display it in any format you like.

这篇关于Coldfusion datepicker dd / mm / yyyy但MySQL保持格式为yyyy / mm / dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆