Q:

date format automatically changed from 'dd/mm/yyyy' to 'mm/dd/yyyy'

so i have this javascript function:
<script  type="text/javascript">

function CompareDates(id)
{

var monName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");

var d = new Date(id);

var curr_date = d.getDate();

var curr_month = d.getMonth();

var curr_year = d.getFullYear();

return (curr_date + " " + monName[curr_month] + " " + curr_year);
}

</script>

and then this calendar tag:
<p:calendar

id="testDate"

styleClass="calendar" 

pattern="d MMM, yyyy"

maxlength="10"

onchange="$(this).val(CompareDates($(this).val()))"

onfocus="$(this).mask('99/99/9999');"

>

<p:watermark for="testDate" value="mm/dd/yyyy" />

</p:calendar>

and for some unknown reason, the default date format that the textbox accepts is in 'mm/dd/yyyy'
For example if i entered "13-05-2014" then it would return an error stating date is invalid.
If i entered "12-05-2014" then it would return "5 Dec, 2014"
I did not declare any dateformat anywhre except for the datepickers which as shown above, is 'd MMM, yyyy'
Before this happened i trialed and error many different kind of codes to try to validate the date however it all didnt work and so i reverted it all back to the original codes. 
Last time the dateformat that the textbox accepted was 'dd/mm/yyyy' and it worked fine with my javascript function except the validation part.
Now it still works except that the dateformat changed to 'mm/dd/yyyy'.
I did try to use console.log to find out what's wrong but there were no error messages.
So could anyone tell me and tell me what is going on here? 
Why has the dateformat changed by itself?
Thank you in advance!

A:

When you do:
var d = new Date(id);

you are effectively calling date.parse, which is largely implementation dependent. Unfortunately the default parser in most browsers expects strings in a format like nn/nn/nnnn to be month/day/year.
You can avoid that by manually parsing the string. If your string is d/m/y then:
function parseDMY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], --b[1], b[0]);
}

You should also provide an on screen tip to tell users what format to use.

