problem saving date to database mysql using jquery datepicker

梦想的初衷 提交于 2019-12-24 23:52:18

问题


this is the code i have used.

<script type="text/javascript">   
    $(document).ready(function(){     
            $("input#txtDate").datepicker({ altField: 'input#txtDate', altFormat: 'yy-dd-mm' }); 
    }); 

</script>

at the time of filling the data to adapter it shows me this error "Unable to convert MySQL date/time value to System.DateTime" how to fix this. i need to show the date in gridview in this format: eg. may-25 2011


回答1:


Datepicker and database are stored data of datetime type and input field is text which is not validated to be proper datetime. Before storing to database convert input text to datetime using:

$("input#txtDate").datepicker("setDate", $("input#txtDate").val())

This will correctly transform input string to datetime. Then store into database the following value of datetime type:

$("input#txtDate").datepicker().val()

or convert it to desired format before.




回答2:


Convert date to format YYYY-MM-DD or in javascript:

$("input#txtDate").datepicker({ altField: 'input#txtDate', altFormat: 'yyyy-mm-dd' }); 



回答3:


This is a reply to Danzan/user755230. Sorry, can't see how to thread comments. ://

For anyone else who stumbles across this - altField and altFormat will create a second, hidden input with a correctly formatted date in it. So you have one formatted for the user (e.g. "02/29/2012" and one formatted for the database ("2012-02-29"). Use the "txtDate" value from the form and all should work right.

It does seem to just need two y's though ('yy-mm-dd').

All the options for the jQuery Datepicker are here: http://docs.jquery.com/UI/Datepicker



来源:https://stackoverflow.com/questions/6092783/problem-saving-date-to-database-mysql-using-jquery-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!