58

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

这是我目前的样机:

$(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>

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

201313-07-03 HH:七月:ss

这是一个JSFiddle

4

10 回答 10

45

试试这个小提琴

$(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);
        }
    });
});
于 2013-07-03T03:13:56.150 回答
41
$("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd ");

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

编辑

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

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

于 2013-07-03T02:55:29.100 回答
36

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

您可以指定dateFormattimeFormat

$('#datepicker').datetimepicker({
    dateFormat: "yy-mm-dd",
    timeFormat:  "hh:mm:ss"
});
于 2014-01-15T18:29:23.893 回答
8

或者,使用 datetimepicker 插件。

参考:DateTimePicker AM/PM 下拉菜单

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

于 2013-12-27T03:54:03.250 回答
1

如果不喜欢添加时间选择器,我们可以只使用 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);
于 2018-04-11T16:10:56.023 回答
1

我添加了这段代码

<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>
于 2018-06-19T17:41:02.277 回答
0

从 npm开始安装:

npm install imask 并导入或要求:

从“imask”导入 IMask;

或使用 CDN:

var dateMask = IMask(element, {
  mask: Date,  // enable date mask

  // other options are optional
  pattern: 'Y-`m-`d',  // Pattern mask with defined blocks, default is 'd{.}`m{.}`Y'
  // you can provide your own blocks definitions, default blocks for date mask are:
  blocks: {
    d: {
      mask: IMask.MaskedRange,
      from: 1,
      to: 31,
      maxLength: 2,
    },
    m: {
      mask: IMask.MaskedRange,
      from: 1,
      to: 12,
      maxLength: 2,
    },
    Y: {
      mask: IMask.MaskedRange,
      from: 1900,
      to: 9999,
    }
  },
  // define date -> str convertion
  format: function (date) {
    var day = date.getDate();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();

    if (day < 10) day = "0" + day;
    if (month < 10) month = "0" + month;

    return [year, month, day].join('-');
  },
  // define str -> date convertion
  parse: function (str) {
    var yearMonthDay = str.split('-');
    return new Date(yearMonthDay[0], yearMonthDay[1] - 1, yearMonthDay[2]);
  },

  // optional interval options
  min: new Date(2000, 0, 1),  // defaults to `1900-01-01`
  max: new Date(2020, 0, 1),  // defaults to `9999-01-01`

  autofix: true,  // defaults to `false`

  // also Pattern options can be set
  lazy: false,

  // and other common options
  overwrite: true  // defaults to `false`
});
于 2020-08-04T01:54:22.870 回答
0

$(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>

于 2020-03-22T16:36:19.567 回答
0

这对我来说很好:

        $('#myelement').datetimepicker({
            dateFormat: "yy-mm-dd",
            timeFormat:  "hh:mm:ss"
        });
于 2020-11-03T11:47:18.457 回答
0

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

于 2019-01-03T15:03:02.933 回答