简体   繁体   English

使用 datetimepicker 插件格式化日期

[英]Formatting date using datetimepicker plugin

I am using the datetimepicker option ( http://trentrichardson.com/examples/timepicker/#basic_examples ).我正在使用 datetimepicker 选项( http://trentrichardson.com/examples/timepicker/#basic_examples )。

$('#basic_example_3').datetimepicker({
    timeFormat: "hh:mm tt"
});

If the user selects no hours or minutes (ie 12/11/2014 12:00 am), I would don't wish to display the time, but just display 12/11/2014.如果用户不选择小时或分钟(即 12/11/2014 12:00 am),我不希望显示时间,而只显示 12/11/2014。 I realize I could use the onClose , and use JavaScript to manually trim the time, but am asking whether there is a better way to do so using the plugin.我意识到我可以使用onClose ,并使用 JavaScript 手动修剪时间,但我想知道是否有更好的方法使用插件来做到这一点。

Also, I will be sending the datetime to the server, and would like it in DATETIME format (ie 2014-12-11 18:30:00).此外,我会将日期时间发送到服务器,并希望它采用 DATETIME 格式(即 2014-12-11 18:30:00)。 I could parse it serverside, however, am asking whether there is aa better way to do so using the plugin.我可以在服务器端解析它,但是,我问是否有更好的方法使用插件来做到这一点。

Not a very elegant solution, but it works.不是一个非常优雅的解决方案,但它有效。 Ideally, moment() wouldn't be used, but some embedded datetimepicker method.理想情况下,不会使用moment() ,而是使用一些嵌入式 datetimepicker 方法。 I tried to do so, but couldn't seem to access hours and minutes.我试图这样做,但似乎无法访问小时和分钟。

$('#basic_example_3').datetimepicker({
    stepMinute: 5,
    timeFormat: "hh:mm tt",
    onClose: function(dateText, inst){
        var datetime = moment(dateText, 'MM/DD/YYYY hh:mm a').format('YYYY-MM-DD HH:mm:ss'); //  12/11/2014 12:00 am => 2014-12-11 18:30:00
        dateText = dateText.replace(/12:00 am$/, '');   //Get rid of minutes if midnight
        $(this).val(dateText);
        $.post('ajax.php',{datetime:datetime},function(json) {
            //display errors if necessary
            }
        );
}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM