简体   繁体   English

如何使用引导日期选择器格式化日期?

[英]How do I format the date with bootstrap datepicker?

I have a select drop down that activates two datepicker input field depending on what is selected. 我有一个选择下拉菜单,根据选择的内容激活两个日期选择器输入字段。 When one field is selected, I would like to fill in the value of today's date plus one year. 选择一个字段后,我想填写今天的日期加上一年的值。 However, when I select that, I get this: Tue Aug 11 2015 00:00:00 GMT-0400 (Eastern Daylight Time) . 但是,当我选择该选项时,我得到的是: Tue Aug 11 2015 00:00:00 GMT-0400(东部夏令时间) I would like to have the date as: mm/dd/yyyy. 我希望日期为:mm / dd / yyyy。

I have setup a fiddle. 我设置了一个小提琴。 http://jsfiddle.net/mwoods98/aku8ytju/ Updated fiddle http://jsfiddle.net/mwoods98/aku8ytju/ 更新了小提琴

Here is a look at the code: 看一下代码:

                 $(document).ready(function() {

                    $('#getHoldStatus').change(function()
                    {
                        var selected_item = $(this).val()
                        if(selected_item == "3"){
                            $('#datepicker').val("").removeClass('invisible');
                            $('#datepicker2').val("").removeClass('invisible');

                            var date = new Date("8/11/2014");
                            var date3 = new Date("8/11/2015");

                            $('#datepicker2').val(date3);
                            var newDate = new Date(date3.getFullYear(), date3.getMonth(), date3.getDate()+1);
                            $('#datepicker2').datepicker({ dateFormat: "mm/dd/yyyy" }); 


                        }
                        else if(selected_item == "2") {
                            $('#datepicker').val("").removeClass('invisible');
                            $('#datepicker2').val(selected_item).addClass('invisible');
                            $("#datepicker2").val('0');
                        }
                        else
                        {
                            $('#datepicker').val(selected_item).addClass('invisible');
                            $('#datepicker2').val(selected_item).addClass('invisible');
                            $("#datepicker").val('');
                            $("#datepicker2").val('');

                        }
                    }
                    );

                        // put all your jQuery goodness in here.
                    });

I am manually setting the date here for now but I can't seem to get it formatted properly. 我现在暂时在此处手动设置日期,但似乎无法正确设置日期格式。

<label class="control-label" for="getHoldStatus"> 
                    Hold Status
                 </label>
                 <div class="controls">
                    <select id="getHoldStatus" name="getHoldStatus">
                          <option value=""></option>

                            <option value="1" >None</option>

                            <option value="3" >On Hold until Candidate Available</option>

                            <option value="2" >On Hold until Position Available</option>

                    </select>
                </div>



                 <label class="control-label" for="input08"> 
                    Hold Start Date
                 </label>
                 <div class="controls">
                    <script>
                        $(function() {
                        $( "#datepicker" ).datepicker({autoclose: true, todayHighlight: true, orientation: "bottom right"});
                          });
                    </script>

                        <div><input type="text" id="datepicker" value="" name="fdfHoldStartDate" class="invisible" /></div>

                </div>



                 <label class="control-label" for="input09"> 
                    Hold End Date
                 </label>
                 <div class="controls">
                    <script>
                        $(function() {
                        $( "#datepicker2" ).datepicker({format: "mm/dd/yyyy", autoclose: true, todayHighlight: true});
                          });
                    </script>

                        <div><input type="text" id="datepicker2" value="" name="fdfHoldEndDate" class="invisible" /></div>

                </div>

UPDATE: For some reason, the fiddle did not update. 更新:由于某种原因,小提琴没有更新。 I have confirmed it and now the source appears relevant to the question I was asking. 我已经确认,现在资料来源似乎与我所问的问题有关。 You can see how the date is being formatted when a selection is made. 您可以查看进行选择时如何格式化日期。

Look at this: Bootstrap 3 datepicker - code generator 看一下这个: Bootstrap 3 datepicker-代码生成器

<input type="text" type="text" id="date1">
$('#date1').datepicker({
     format: "mm-dd-yyyy"
});

Use the format instead of formatDate : 使用format代替formatDate

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

The check in/check out example should be useful: Datepicker for Bootstrap 签入/签出示例应该很有用: Bootstrap的Datepicker

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

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