简体   繁体   中英

How compare two dates?

I have a two date like from and to date. date format like 28-Sep-2013 and 01-Sep-2013 . I want to compare date and show error message. I have use this code,

     var from = $('#<%= txtFDateEdu.ClientID %>').val();
     var to = $('#<%= txtTDateEdu.ClientID %>').val();

     var dateTypeVar = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
     var dateTypeVarto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
     $.datepicker.formatDate('dd-mm-yy', dateTypeVar);

     var datefrom = $.datepicker.formatDate('dd/mm/yyyy', dateTypeVar);
     var dateto = $.datepicker.formatDate('dd/mm/yyyy', dateTypeVarto);
         alert(datefrom);    

         if (datefrom > dateto) {
             alert("Success");
          }
         else {

         }

This code is not working properly. Some issue occur in date format. Please any one help me to solve date format issue and compare date properly. i also use this code.

var from = $('#<%= txtFDateEdu.ClientID %>').val();
                var to = $('#<%= txtTDateEdu.ClientID %>').val(;)

                alert(from);
                alert(to);
                var datefrom = new Date(from);
                var dateto = new Date(to);
                alert(datefrom);
                 alert(dateto);
                if (datefrom > dateto) {
                    alwer("Success");
                }
                else {

                }

but this also not work properly.

You are converting a given date to a string, just skip this step:

 var datefrom = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
 var dateto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');

     if (datefrom > dateto) {
         alert("Success");
      }
     else {

     }

Just compare the two Date objects before you've converted them to strings:

var dateTypeVar = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');
var dateTypeVarto = $('#<%= txtFDateEdu.ClientID %>').datepicker('getDate');

if (dateTypeVar > dateTypeVarto) {
    ...
}

The comparison operator will automatically take the value of the two date objects via .valueOf() , that being the milliseconds elapsed since 1970/01/01 00:00:00.

I also use date picker, normally i use following script.

   function compare_dates(valid_to, valid_from){
  var splited_to = valid_to.split("-")
  var to_date_monthfield=splited_to[1];
  var to_date_dayfield=splited_to[0];
  var to_date_yearfield=splited_to[2];
  var to_date = new Date(to_date_yearfield, to_date_monthfield-1, to_date_dayfield);

  var splited_from = valid_from.split("-");
  var from_date_monthfield=splited_from[1];
  var from_date_dayfield=splited_from[0];
  var from_date_yearfield=splited_from[2];
  var from_date = new Date(from_date_yearfield, from_date_monthfield-1, from_date_dayfield);

  var cur_date = new Date();
  if(!(from_date > cur_date.setDate(cur_date.getDate() - 1))){
    return false;
  }
  if(!(to_date > from_date)){
    return false;
  }
  return true;

}

This expects your date date to be in dd//mm/yyyy format

here i found something use full. You can use it with my code.

new Date(Date.parse(month_string +" 1, 2012")).getMonth()+1

you can replace monthfield with above code. or you also can use a other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM