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

JavascriptJqueryJquery UiCalendar

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

Use the dateFormat option

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

Solution 2 - Javascript

Try the following:

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

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

Solution 3 - Javascript

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'  > 

Solution 4 - Javascript

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

See: http://jqueryui.com/demos/datepicker/ and http://docs.jquery.com/UI/Datepicker/formatDate#utility-formatDate

Solution 5 - Javascript

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

<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:

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

Solution 6 - Javascript

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

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

Solution 7 - Javascript

this also worked for me. Go to the 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
	};

Solution 8 - Javascript

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);

Solution 9 - Javascript

Try this:

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

Solution 10 - Javascript

Use .formatDate( format, date, settings )

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

Solution 11 - Javascript

just add dateFormat:'yy-mm-dd' to your .datepicker({}) settings, your .datepicker({}) can look something like this

			$( "#datepicker" ).datepicker({
				showButtonPanel: true,
				changeMonth: true,
				dateFormat: 'yy-mm-dd'
			});
          });

	</script> 

Solution 12 - Javascript

Just need to define yy-mm-dd here. dateFormat

Default is mm-dd-yy

Change mm-dd-yy to yy-mm-dd. Look example below

  $(function() {
    $( "#datepicker" ).datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
  } );

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

Solution 13 - Javascript

Thanks for all. I got my expected output

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>

<script>
$(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 );
        })
    });

</script> 
    
<div class="">
    <p>From Date: <input type="text" id="from"></p>
    
    <p>To Date: <input type="text" id="to"></p>
</div>

Solution 14 - Javascript

this worked for me.

    $('#thedate').datepicker({
    dateFormat: 'dd-mm-yy',
    altField: '#thealtdate',
    altFormat: 'yy-mm-dd'
});

Solution 15 - Javascript

This work for me:

 $('#data_compra').daterangepicker({
          singleDatePicker: true,
          locale: {
           format: 'DD/MM/YYYY'
          },
         calender_style: "picker_4", }, 
function(start, end, label) {
          console.log(start.toISOString(), end.toISOString(), label); 
});

Solution 16 - Javascript

This is what worked for me in every datePicker version, firstly converting date into internal datePicker date format, and then converting it back to desired one

var date = "2017-11-07";   
date = $.datepicker.formatDate("dd.mm.yy", $.datepicker.parseDate('yy-mm-dd', date));
// 07.11.2017

Solution 17 - Javascript

For me in datetimepicker jquery plugin format:'d/m/Y' option is worked

$("#dobDate").datetimepicker({
lang:'en',
timepicker:false,
autoclose: true,
format:'d/m/Y',
onChangeDateTime:function( ct ){
   $(".xdsoft_datetimepicker").hide();
}
});

Solution 18 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn KenView Question on Stackoverflow
Solution 1 - JavascriptGabriele PetrioliView Answer on Stackoverflow
Solution 2 - JavascriptGazillionView Answer on Stackoverflow
Solution 3 - JavascriptShikoView Answer on Stackoverflow
Solution 4 - JavascriptarychjView Answer on Stackoverflow
Solution 5 - JavascriptMohsin ShoukatView Answer on Stackoverflow
Solution 6 - JavascriptHuskeView Answer on Stackoverflow
Solution 7 - Javascriptgani024View Answer on Stackoverflow
Solution 8 - JavascriptsiwalikmView Answer on Stackoverflow
Solution 9 - JavascriptPeng QiView Answer on Stackoverflow
Solution 10 - JavascriptVlad DneprovskiyView Answer on Stackoverflow
Solution 11 - JavascriptK.R.JanssenView Answer on Stackoverflow
Solution 12 - JavascriptRokan NashpView Answer on Stackoverflow
Solution 13 - JavascriptJohn KenView Answer on Stackoverflow
Solution 14 - JavascriptNarendra KothuleView Answer on Stackoverflow
Solution 15 - JavascriptEdiano GamaView Answer on Stackoverflow
Solution 16 - JavascriptFantomX1View Answer on Stackoverflow
Solution 17 - JavascriptsuryaView Answer on Stackoverflow
Solution 18 - JavascriptMehmet ÇetinView Answer on Stackoverflow