Formatting Date With jQuery Date Picker

Using jQuery date-picker we can format a selected date by using dateFormat option. In previous post we have seen how to use jQuery datepicker in a form to select value for a date field. It popup a calendar widget to select date.

In this tutorial, we are going to see how to format a date on picking it from the calendar popup. We have a list of date format in a select box. On changing this select value, We set dateFormat option with a selected format.

View Demo


HTML Date Picker with Formats

This code shows the HTML for displaying an inline datepicker field and a list of date format.

<div id="frm-date-format">
	<div class="frm-input-row">
		<div class="frm-label">Select Date:</div>
		<div><input type="text" id="datepicker" size="30" class="form-input"></div> 
	<div class="frm-input-row">
			<select id="date-format" onchange="setDateFormat(this.value);" class="form-input">
				<option value="mm/dd/yy">mm/dd/yy</option>
				<option value="dd/mm/yy">dd/mm/yy</option>
				<option value="yy-mm-dd">yy-mm-dd</option>
				<option value="M d, y">M d, y</option>

Setting dateFormat using jQuery

This jQuery code is used to set the date format on selecting date using datepicker. We have a function to format date which will be called on changing date-format select field.

	$(document).ready(function() {
		$( "#datepicker" ).datepicker();
	function setDateFormat(date_format) {
		$( "#datepicker" ).datepicker( "option", "dateFormat", date_format );

View DemoDownload

This jQuery code tutorial was published on June 26, 2016.

↑ Back to Top