比较JavaScript中三个数组的数据

时间:2014-09-07 13:42:23

标签: javascript arrays compare

我需要制作某种计算器,我想在使用三种不同的阵列时这样做...

var = Month; 
var = Num_of_Month;
var = Days_of_Month;
var = Year;

  Month = new Array("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
  Num_of_Month = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  Days_of_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
  Year = new Array(2014, 2015, 2016);

如何比较它们?

1 个答案:

答案 0 :(得分:0)

如果您只想获取用户选择的日期并计算它们之间的天数,并且如果您确实使用了datepicker,那么您可以在不使用3个数组的情况下执行此操作。

请看这个fiddle以获得一个想法。

$("#StartDate").datepicker({
    changeMonth: true,
    changeYear: true,
});
$("#EndDate").datepicker({
    changeMonth: true,
    changeYear: true,        
    onSelect: function () {
        datediff();
    }
});

function datediff() {
    if (($('#StartDate').val().length !== 0) && ($('#EndDate').val().length !== 0)) {

        var StartDate = $('#StartDate').datepicker('getDate');
        var EndDate = $('#EndDate').datepicker('getDate');
        //Divide by 1000ms, 60secs, 60mins, 24hours - you get number of days.
        var numOfDays = Math.abs(new Date(EndDate - StartDate))/1000/60/60/24;
        alert(numOfDays + ' Days');
    }
}