Reputation: 6938
I have gone through the questions asked about date format in stackoverflow and i have found numerous ones marked duplicate. I am posting this after trying out various methods that they have accepted as answer but i cannot somehow get it to work.
First of all i am using backbone.
What i have is a date format in (Y-m-d) which comes from database. Through json parsing i get it into the underscore template. I need it to be in (m-d-Y) format.
Value from database
data.pp.dob
What i tried (to convert it into m-d-Y format)
var date = new Date(data.pp.dob);
dateNew = (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getFullYear();
I am using jquery datepicker plugin to get the calender. So everything is going wierd when i try to format date. Sometimes it will give NAN when i try to trigger calender. Sometimes it will give the year like in 4400 and all..
$('.date-picker').datepicker();
HTML code
<%
var date = new Date(data.pp.dob);
dateNew = (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getFullYear();
%>
<%=dateNew%>
<input value="<%=dateNew%>" class="span5 date-picker dob" name="dob" id="id-date-picker-1" type="text" data-date-format="yyyy-mm-dd" />
Upvotes: 0
Views: 3764
Reputation: 2925
Assuming date you receive from backend is correct, most probable issue lies here:
<input value="<%=dateNew%>" class="span5 date-picker dob" name="dob" id="id-date-picker-1" type="text" data-date-format="yyyy-mm-dd" />
Datepicker widget has some strange convention:
So, format for input should be: yy-mm-dd documentation: http://api.jqueryui.com/datepicker/
Upvotes: 4