The getDate method of datepicker returns a date type, not a string.
You need to format the returned value to a string using your date format. Use datepicker's formatDate function:
var startDate = $('#MilestoneStartDate').datepicker('getDate');
$.datepicker.formatDate('dd M yy', startDate);
The full list of format specifiers is available here.
EDIT
$('#MilestoneStartDate').datepicker("getDate");
Always give you the correct date if date is selected by mouse to click on a date popup but if someone manually write or paste date in different format this gives you the current date.
So to handle this situation use bellow code.
$(function(){
$('#MilestoneStartDate').datepicker({
dateFormat: 'dd M yy'
});
});
var strSelectedDate = new Date($('#MilestoneStartDate').val());
var formatedDate = $.datepicker.formatDate('dd M yy', strSelectedDate);
alert(formatedDate);
Working Demo
As per @Boris Serebrov comment
The key is new Date($('#MilestoneStartDate').val()) - it actually tries to "guess" the format, so things like "2 Mar 2016", "March 2 2016" or "2016, 2 March" will be parsed correctly.
Step by step:
Case 1:
Here strSelectedDate
give you the date in standard format lets say if i select 02 Mar 2016
from date popup it give
Wed Mar 02 2016 00:00:00 GMT+0530
Now
$.datepicker.formatDate('dd M yy', strSelectedDate);
give you the date of your format like bellow
02 Mar 2016
Case:2
As you mention in your question if user manually enter date like 1/1/2016 strSelectedDate
give you the standard date like bellow
Fri Jan 01 2016 00:00:00 GMT+0530
Then
$.datepicker.formatDate('dd M yy', strSelectedDate);
give you the formated date
01 Jan 2016
Which is correct date as expected.
Case 3:
Now if user write some invalid date manually like 112016
then strSelectedDate
returns Invalid Date
so here you can implement some client side validation to the user to enter the correct date.
Hope this helps you.
(MM/DD/YYYY)
. Then just show a validation error when the input doesn't match the format. This is much easier than trying to parse multiple formats, and still allows user input instead of select menus. – Nothingness