4

我遇到了一些问题来获得正确的日期格式。

在我的日期字段失去焦点后,我希望它显示如下:

dd-mm-yyyy 但它必须像这样保存在数据库中

yyyy-mm-dd。.

这是我到目前为止所拥有的:

$("#birthdate").datepicker({
            changeMonth: true,
            changeYear: true,
            altFormat: 'yyyy-mm-dd' 
             });

现在它显示在日期字段中,如 2009 年 11 月 25 日(2009 年 11 月 25 日),发布日期为 2009 年 11 月 25 日

有任何想法吗?

4

2 回答 2

4

尝试这个:

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

<input type="text" name="datepicker" value="" id="datepicker">
<input type="hidden" name="realdate" value="" id="realdate">

然后使用 realdate 获取发布日期。

于 2009-04-12T12:00:17.887 回答
2

MrHus 有一个很好的答案,但您应该只在后端处理它,因为这些信息可以通过代理或许多其他可能的恶意方式伪造。例如,如果您正在运行 PHP,它将是这样的:

<?php
// BTW... you'll want to check for XSS attacks and other vulnerabilities first...
$date = '08/05/1986';//$_REQUEST['datepicker'];
// This is very simple... you should also check if the date is real...
if(preg_match('/^\d{2}\/\d{2}\/\d{4}$/',$date)) {
    $date_formatted = date('Y-m-d',strtotime($date));
}
// put $date_formatted into database...
?>

我们的两个答案的组合应该让你(大部分)安全地到达那里。

于 2009-04-15T16:06:26.210 回答