使用 jQuery UI Datepicker 时如何用小时、分钟和秒来格式化日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17438857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:04:11  来源:igfitidea点击:

How to format date with hours, minutes and seconds when using jQuery UI Datepicker?

javascriptjquerydatepickerjquery-ui-datepicker

提问by NETCreator Hosting

Is it possible to format a date with jQuery UI Datepickeras to show hours, minutes and seconds?

是否可以使用jQuery UI Datepicker格式化日期以显示小时、分钟和秒?

This is my current mockup:

这是我目前的模型:

$(function() {
    $('#datepicker').datepicker({ dateFormat: 'yyy-dd-mm HH:MM:ss' }).val();
});
<html>
<head>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
  <title>snippet</title>
</head>
<body>

  <input type="text" id="datepicker">

  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

When I call .datepicker({ dateFormat: 'yyy-dd-mm HH:MM:ss' })the returned value is:

当我调用.datepicker({ dateFormat: 'yyy-dd-mm HH:MM:ss' })返回的值是:

201313-07-03 HH:July:ss

201313-07-03 HH:7月:ss

Here is a JSFiddle.

这是一个JSFiddle

采纳答案by scel.pi

$("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd ");

For the time picker, you should add timepicker to Datepicker, and it would be formatted with one equivalent command.

对于时间选择器,您应该将 timepicker 添加到 Datepicker,它将使用一个等效命令进行格式化。

EDIT

编辑

Use this one that extend jQuery UI Datepicker. You can pick up both date and time.

使用扩展 jQuery UI Datepicker 的这个。您可以选择日期和时间。

http://trentrichardson.com/examples/timepicker/

http://trentrichardson.com/examples/timepicker/

回答by Arun Unnikrishnan

Try this fiddle

试试这个小提琴

$(function() {
    $('#datepicker').datepicker({
        dateFormat: 'yy-dd-mm',
        onSelect: function(datetext) {
            var d = new Date(); // for now

            var h = d.getHours();
            h = (h < 10) ? ("0" + h) : h ;

            var m = d.getMinutes();
            m = (m < 10) ? ("0" + m) : m ;

            var s = d.getSeconds();
            s = (s < 10) ? ("0" + s) : s ;

            datetext = datetext + " " + h + ":" + m + ":" + s;

            $('#datepicker').val(datetext);
        }
    });
});

回答by Miguel Stevens

Using jQuery UI in combination with the excellent datetimepicker plugin from http://trentrichardson.com/examples/timepicker/

将 jQuery UI 与来自http://trentrichardson.com/examples/timepicker/的优秀 datetimepicker 插件结合使用

You can specify the dateFormatand timeFormat

您可以指定dateFormattimeFormat

$('#datepicker').datetimepicker({
    dateFormat: "yy-mm-dd",
    timeFormat:  "hh:mm:ss"
});

回答by Cray Kao

Or, using datetimepicker plugin.

或者,使用 datetimepicker 插件。

Ref: DateTimePicker AM/PM Dropdown

参考:DateTimePicker AM/PM 下拉菜单

Ref: http://xdsoft.net/jqplugins/datetimepicker/

参考:http: //xdsoft.net/jqplugins/datetimepicker/

回答by ???? ????? ?????? ???? ? ?????

I added this code

我添加了这个代码

<input class="form-control input-small hasDatepicker" id="datepicker6" name="expire_date" type="text" value="2018-03-17 00:00:00">

<script src="/assets/js/datepicker/bootstrap-datepicker.js"></script>
<script>
        $(document).ready(function() {
            $("#datepicker6").datepicker({
                isRTL: true,
                dateFormat: "yy/mm/dd 23:59:59",
                changeMonth: true,
                changeYear: true

            });
        });
</script>

回答by Salam

format: 'YYYY-MM-DD HH:mm:ss',

format: 'YYYY-MM-DD HH:mm:ss',

回答by DipGhosh

$(function() {
    $('#datepicker').datepicker({ dateFormat: 'yy-d-m ' }).val();
});
<html>
<head>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
  <title>snippet</title>
</head>
<body>

  <input type="text" id="datepicker">

  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

回答by vusan

If don't like to add time picker, we can use just javascript for time part.

如果不喜欢添加时间选择器,我们可以只使用 javascript 作为时间部分。

  var dateObj=new Date();
  var date = $.datepicker.formatDate('dd M yy', dateObj);
  var time  = dateObj.getHours()+":"+dateObj.getMinutes()+":"+dateObj.getSeconds(); 

  console.log(date," ",time);