Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
var startDate=document.getElementById("<%= txtStartDate.ClientID %>").value ;
       var endDate= document.getElementById("<%= txtEndDate.ClientID %>").value;
         var str = Date.parse(startDate).toString();
         var str1 = Date.parse(endDate).toString();

        if (str >= str1) {
            alert("End Date must be greater than Start Date");
            document.getElementById("<%= txtEndDate.ClientID %>").focus();
            return false;
        }



My Date Format is : dd-MMM-yyyy i.e. 9-Jul-2014
Posted
Comments
ashok rathod 16-Jul-14 6:20am    
what exactly is not working can u tell us ?
Neha Mukesh 16-Jul-14 6:48am    
mozilla firefox does not show alert
I think Date.parse is not working in Mozilla
Kornfeld Eliyahu Peter 16-Jul-14 7:12am    
Read here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
agent_kruger 16-Jul-14 8:50am    
is it working in other browsers?

1 solution

Date.parse can recognize only specific formats for specific browsers, and also we don't have the option for mention the input format anywhere using this method.
In that case it thinks that the input is in the format mm/dd/yyyy, so the result is wrong.

To fix the issue we need either to parse the input manually by splitting each part and then construct a Date object or we may use some already exisitng library which will take care of this like date.js/moment.js etc.

Example for manual parsing:

JavaScript
var inputDate = $('#' + controlName).val();
var parts = inputDate.split("-");
var d = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));


Example using date.js:

JavaScript
var inputDate = $('#' + controlName).val();
var d = Date.parseExact(inputDate , "dd/MM/yyyy");



Hope this will be of help or show you some hint to go forward.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900