最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bootstrap-datetimepicker show date only - Stack Overflow

programmeradmin4浏览0评论

I am using this repo by smalot and I want to select and show date only (for some other places I am showing data and time, therefore selecting this repo). I can manage to use it for selecting date only but the generated date string is always yyyy-mm-dd hh:ii. And if I did not understand wrongly the format option is for parsing the initial date only but not setting dates.

<div class="input-group date datetime dateonly" data-start-view="2" data-min-view="2" style="margin-bottom: 0;">
    <input class="form-control" size="16" type="text" placeholder="Release Date" id="release-date" name="release_date" value="{{ unit.release_dt|date:'Y-m-d' }}" style="cursor: default; background-color: #ffffff;">
    <span class="input-group-addon btn btn-primary"><span class="glyphicon glyphicon-th"></span></span>
</div>

I am setting startView and minView to be both 2 so that the user will need to select date only but I still got 2015-09-22 00:00 as the output-- I just want it to show 2015-09-22.

In fact, by listening to changeDate and hide events, I can just set the value of date inputs. I am wondering if there is a less hacky and dirty way of doing it

$('.datetimepicker').on('changeDate', function(e) {//get substring and set date input
}).on('hide'....//same thing
);

I am using this repo by smalot and I want to select and show date only (for some other places I am showing data and time, therefore selecting this repo). I can manage to use it for selecting date only but the generated date string is always yyyy-mm-dd hh:ii. And if I did not understand wrongly the format option is for parsing the initial date only but not setting dates.

<div class="input-group date datetime dateonly" data-start-view="2" data-min-view="2" style="margin-bottom: 0;">
    <input class="form-control" size="16" type="text" placeholder="Release Date" id="release-date" name="release_date" value="{{ unit.release_dt|date:'Y-m-d' }}" style="cursor: default; background-color: #ffffff;">
    <span class="input-group-addon btn btn-primary"><span class="glyphicon glyphicon-th"></span></span>
</div>

I am setting startView and minView to be both 2 so that the user will need to select date only but I still got 2015-09-22 00:00 as the output-- I just want it to show 2015-09-22.

In fact, by listening to changeDate and hide events, I can just set the value of date inputs. I am wondering if there is a less hacky and dirty way of doing it

$('.datetimepicker').on('changeDate', function(e) {//get substring and set date input
}).on('hide'....//same thing
);
Share Improve this question edited Aug 24, 2017 at 13:07 VincenzoC 31.5k12 gold badges100 silver badges121 bronze badges asked Dec 30, 2015 at 8:25 Junchao GuJunchao Gu 1,8656 gold badges30 silver badges43 bronze badges 4
  • The format will remain for dates user will select, not only for setting initial dates.. – Rayon Commented Dec 30, 2015 at 8:30
  • could you create a jsfiddle, jsfiddle – dreamweiver Commented Dec 30, 2015 at 8:39
  • Specify format for date like, $('#datetimepicker').datetimepicker({ format: 'yyyy-mm-dd' }); Also, can you create fiddle for it to investigate? – Parkash Kumar Commented Dec 30, 2015 at 8:47
  • Sadly to say this but the library is on my puter and I do not have any idea which version it is using(existing code). – Junchao Gu Commented Dec 30, 2015 at 9:41
Add a ment  | 

4 Answers 4

Reset to default 5

Use date picker

$("#calender").datepicker({
    format: "mm/dd/yyyy"
});

To show date only use

format: 'L'

To show Time only use

format: 'LT'

Reference

https://github./Eonasdan/bootstrap-datetimepicker/issues/832

you will edit datetaimepicker.js

var defaults = $.fn.datepicker.defaults = {
    autoclose: false,
    beforeShowDay: $.noop,
    calendarWeeks: false,
    clearBtn: false,
    daysOfWeekDisabled: [],
    endDate: Infinity,
    forceParse: true,
    //format: 'mm/dd/yyyy', 时间输出格式
    format: 'yyyy-mm-dd',
    keyboardNavigation: true,
    language: 'en',
    minViewMode: 0,
    multidate: false,
    multidateSeparator: ',',
    orientation: "auto",
    rtl: false,
    startDate: -Infinity,
    startView: 0,
    todayBtn: false,
    todayHighlight: false,
    weekStart: 0
};

like this

    //format: 'mm/dd/yyyy', 时间输出格式
    format: 'yyyy-mm-dd'

wish can help you!

Try this - HTML:

<div class="controls input-append date m" data-date="" data-date-format="yyyy-mm-dd" data-link-field="m1" data-link-format="yyyy-mm-dd" data-start-view="2" data-min-view="2">
<input size="16" id="m" type="text" value="" readonly>
<span class="add-on"><i class="icon-remove"></i></span>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<input type="hidden" id="m1" value="" />

JS:

$('.m').on('changeDate', function(e) {
            console.log($("#m").val());
        });

Console:

2015-12-08

Hope it will help.

发布评论

评论列表(0)

  1. 暂无评论