-2

I have a datepicker which works, but I am trying to convert it to a date object so that i could getDay from it. Problem is everytime i convert it, it gives me the wrong format, as in it will be MM/dd/yy, rather than dd/MM/yy.

<script type="text/javascript">
$(document).ready(function () {
    $('#<%=txtDateTime.ClientID%>').datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function (date) {
            alert(new Date(Date.parse(date))); //this keeps giving month/day/year
        }
    });
});

Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
user1764339
  • 9
  • 1
  • 4

2 Answers2

0

Use datepicker's parseDate parse the custom formatted date

$(document).ready(function () {
    $('#test').datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function (date) {
            var dt = $.datepicker.parseDate( 'dd/mm/yy', date)
            alert(dt.getDate()); 
        }
    });
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

The Date.parse method is implementation dependent. new Date(string) is equivalent to Date.parse(string).

I would recommend to parse the date string manually, and use the Date constructor with the year, month and day arguments to avoid ambiguity:

// parse a date in yyyy-mm-dd format
function parseDate(input) {
  var parts = input.split('/');
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48