问题
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.
回答1:
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.
回答2:
(Moved from comments for greater visibility)
One important addition to Adrian's answer. In this specific case, you MUST use a date mask with parseDateTime(). The mask controls how parseDateTime() interprets the input. Without the correct mask, the results may be wrong for your specific input, namely dd/mm/yyyy.
TryCF Example
Code:
<cfscript>
dateString = "05/08/2018";
writeOutput("<br>Without mask = "& parseDateTime( dateString) );
writeOutput("<br>With mask = "& parseDateTime( dateString, "dd/MM/yyyy") );
</cfscript>
Result:
Without mask = {ts '2018-05-08 00:00:00'} (May 8, 2018)
With mask = {ts '2018-08-05 00:00:00'} (August 5, 2018)
来源:https://stackoverflow.com/questions/49229991/coldfusion-datepicker-dd-mm-yyyy-but-mysql-stay-with-format-yyyy-mm-dd