I am trying to compare start date and end date in jquery. Code in aspx page looks like:
<asp:TextBox ID="tbStartDate" runat="server" CssClass="textbox" Width="80px" contentEditable="false" onchange="javascript:compareDates();"></asp:TextBox>
<asp:ImageButton runat="Server" ID="ibStartCalendar" ImageUrl="~/Images/calendar.jpg"
Height="18px" AlternateText="Click to show calendar" />
<act:CalendarExtender ID="ceStartDate" runat="server" Format="dd-MMM-yyyy" TargetControlID="tbStartDate" PopupButtonID="ibStartCalendar" CssClass="Calendar">
</act:CalendarExtender>
And javascript function:
function compareDates() {
var start = $("#<%=tbStartDate.ClientID %>").val();
var end = $("#<%=tbEndDate.ClientID %>").val();
if (!compareDate(start, end)) {
$("#<%=lblMsg.ClientID %>").html("Start Date can not be greater than End Date");
}
}
function compareDate(start, end) {
if (start.length > 0 && end.length > 0) {
var stDate = new Date(start);
var enDate = new Date(end);
var compDate = enDate - stDate;
if (compDate >= 0)
return true;
else {
return false;
}
} else { return true; }
}
Requirement is to dispaly date in "dd-MMM-yyyy" format in text boxes to I can not change format in calenderExtender.
When I keep this format star date and end date becomes "NaN". If I change format in calenderExtender i get proper results. However as I said I can not change format there. How can I change format in javascript?
Here is the solution,
i have made custom date conversion function
GetDate
to convertdd-MMM-yyyy
format string to javascript Date object.Kapil,
Improve method of your functionality will be
}
this will return date in dd-mm-yyyy format.
You can use jquery date-format plugin to format date-time very effectively and customize as you want to.
Or, you can use jqueryUI as well to format date.
$.datepicker.parseDate( format, value, settings )
Here is the Answer...