Comparing dates in jquery
Asked Answered
D

7

14

I am having the following codes and it though i'm having 01-Jan-2009 for DateTo and 03-Jan-2009 for DateFrom it's reading the values as NAN. Am I missing anything? I`m referencing

 var DateToValue = $("#DateTo").val();
      var DateFromValue = $("#DateFrom").val();

      if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
          $("#DateFrom").val(DateToValue)
      }
  <script src="@Url.Content("~/Scripts/jquery-1.4.2.min.js")"
 type="text/javascript"></script>

 <script  src="@Url.Content("~/Scripts/jquery.datePicker.js")"
type="text/javascript"></script>

         <script src="@Url.Content("~/Scripts/jquery.validate.js")"
 type="text/javascript"></script>
Desmond answered 15/2, 2011 at 6:54 Comment(0)
U
6

Use DateJS for parsing your date. http://www.datejs.com/ just include the script in your html.

Ubana answered 15/2, 2011 at 6:58 Comment(0)
B
19

How about this?

  DateTime DateToValue = $("#DateTo").val();
  DateTime DateFromValue = $("#DateFrom").val();

  if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
      $("#DateFrom").val(DateToValue)
  }
Baccate answered 15/2, 2011 at 7:5 Comment(0)
S
16

The Easy Way to Do is

 var start= new Date($('#txtstart').val());
 var end= new Date($('#txtend').val());
            if (start < end) {

            }
Squib answered 1/8, 2013 at 5:44 Comment(0)
U
6

Use DateJS for parsing your date. http://www.datejs.com/ just include the script in your html.

Ubana answered 15/2, 2011 at 6:58 Comment(0)
M
3

My Comparison with the current date

    function isPastDate(dateText) {
// date is dd/mm/yyyy
        var inputDate = dateText.split("/");
        var today = new Date();
        inputDate = new Date(inputDate[2], inputDate[1] - 1, inputDate[0], 0, 0, 0, 0);
        today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
        return inputDate < today;
};
Medea answered 9/11, 2015 at 5:13 Comment(0)
L
1

I think my answer is late, tell me if it is better or not as only this worked for me in v1.10:

if($('#DateTo').val().split("/").reverse().join("") <= 
$('#DateFrom').val().split("/").reverse().join("")){//Do something}
Links answered 28/5, 2018 at 7:46 Comment(0)
P
0

you can use below code to parse through the dates, using the millisecond approach (adding milliseconds present in a day) will not work properly for the daylightsaving.

for ( beginDate= new Date(startDate.getTime()); beginDate.getTime()<=endDate.getTime(); beginDate.setDate(beginDate.getDate() + 1)) {
            dateRangeArray.push(new Date(beginDate.getTime()));
        }
Patel answered 12/3, 2013 at 20:48 Comment(0)
R
0

here's more a simplified way from simple java script

<script>
 //get the values
function asdf(){
var fDate=document.forms[0].fromDate.value;
var tDate=document.forms[0].toDate.value;

//Compare

if(fDate>tDate){
        var dateComponentsfrom = fDate.split("/");

//split and convert into date to be more precise

        var fromdate = new Date(dateComponentsfrom[2], dateComponentsfrom[1] - 1, dateComponentsfrom[0]);
        var dateComponentsto = tDate.split("/");
        var todate = new Date(dateComponentsto[2], dateComponentsto[1] - 1, dateComponentsto[0]);
        if(fromdate>todate){
            alert('from date cannot be greater than to date');
            return false;                        
        }
    }
}
</script>
Rhoea answered 1/2, 2014 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.