Javascript 如何在日期选择器中将日期格式 (MM/DD/YY) 更改为 (YYYY-MM-DD)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7500058/
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-24 02:31:24  来源:igfitidea点击:

How to change date format (MM/DD/YY) to (YYYY-MM-DD) in date picker

javascriptjqueryjquery-uicalendar

提问by John Ken

I have following datepicker script:

我有以下日期选择器脚本:

<script>
 $(function(){
        $("#to").datepicker();
        $("#from").datepicker().bind("change",function(){
            var minValue = $(this).val();
            minValue = $.datepicker.parseDate("mm/dd/yy", minValue);
            minValue.setDate(minValue.getDate()+1);
            $("#to").datepicker( "option", "minDate", minValue );
        })
    });
</script> 

Now dateformat is MM/DD/YY .how to change the date format to YYYY-MM-DD?

现在日期格式是 MM/DD/YY 。如何将日期格式更改为 YYYY-MM-DD?

回答by Gabriele Petrioli

Use the dateFormatoption

使用dateFormat选项

 $(function(){
        $("#to").datepicker({ dateFormat: 'yy-mm-dd' });
        $("#from").datepicker({ dateFormat: 'yy-mm-dd' }).bind("change",function(){
            var minValue = $(this).val();
            minValue = $.datepicker.parseDate("yy-mm-dd", minValue);
            minValue.setDate(minValue.getDate()+1);
            $("#to").datepicker( "option", "minDate", minValue );
        })
    });

Demo at http://jsfiddle.net/gaby/WArtA/

演示在http://jsfiddle.net/gaby/WArtA/

回答by Gazillion

Try the following:

请尝试以下操作:

$('#to').datepicker({
      dateFormat: 'yy-mm-dd'
});

You'd think it would be yyyy-mm-dd but oh well :P

你会认为它会是 yyyy-mm-dd 但是哦 :P

回答by Shiko

I had the same issue and I have tried many answers but nothing worked.

我有同样的问题,我尝试了很多答案,但没有任何效果。

I tried the following and it worked successfully :

我尝试了以下方法并且成功了:

<input type=text  data-date-format='yy-mm-dd'  > 

回答by Mohsin Shoukat

If in jquery the dateformat option is not working then we can handle this situation in html page in input field of your date:

如果在 jquery 中 dateformat 选项不起作用,那么我们可以在日期输入字段的 html 页面中处理这种情况:

<input type="text" data-date-format='yyyy-mm-dd'  id="selectdateadmin" class="form-control" required>

And in javascript below this page add your date picker code:

并在此页面下方的 javascript 中添加您的日期选择器代码:

 $('#selectdateadmin').focusin( function()
     {
       $("#selectdateadmin").datepicker();
       
     });

回答by gani024

this also worked for me. Go to the bootstrap-datepicker.js.

这也对我有用。转到bootstrap-datepicker.js

replace this code :

替换此代码:

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

with :

和 :

var defaults = $.fn.datepicker.defaults = {
  autoclose: false,
  beforeShowDay: $.noop,
  calendarWeeks: false,
  clearBtn: false,
  daysOfWeekDisabled: [],
  endDate: Infinity,
  forceParse: true,
  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
 };

回答by siwalikm

I used this approach to perform the same operation in my app.

我使用这种方法在我的应用程序中执行相同的操作。

var varDate = $("#dateStart").val(); 

var DateinISO = $.datepicker.parseDate('mm/dd/yy', varDate); 

var DateNewFormat = $.datepicker.formatDate( "yy-mm-dd", new Date( DateinISO ) );

$("#dateStartNewFormat").val(DateNewFormat);

回答by Husein Roncevic

You need something like this during the initialization of your datepicker:

在您的日期选择器初始化期间,您需要这样的东西:

$("#your_elements_id").datepicker({ dateFormat: 'yyyy-mm-dd' });

回答by Vlad Dneprovskiy

Use .formatDate( format, date, settings )

.formatDate( format, date, settings )

http://docs.jquery.com/UI/Datepicker/formatDate

http://docs.jquery.com/UI/Datepicker/formatDate

回答by Peng Qi

Try this:

尝试这个:

$.datepicker.parseDate("yy-mm-dd", minValue);