【问题标题】:How to get max and min dates from array using javascript/jquery?如何使用 javascript/jquery 从数组中获取最大和最小日期?
【发布时间】:2017-02-22 10:19:33
【问题描述】:

我想从一个 json 数组中获取最小和最大日期:

我的代码

$.getJSON(url, function (data) {
    var dates = Object.keys( data['data']['horodate'] ).map(function ( key ) {
        return data['data']['horodate'][key].replace(/\-/g,'/' ) 
    });
    var min = new Date(Math.min.apply( null, dates ));
    var max =  new Date(Math.max.apply( null, dates));
});

data 数组是:

Array [ 
    "2016/10/13 00:00:00", 
    "2016/10/13 00:30:00", 
    "2016/10/13 01:00:00", 
    "2016/10/13 01:30:00", 
    "2016/10/13 02:00:00", 
    "2016/10/13 02:30:00", 
    "2016/10/13 03:00:00", 
    "2016/10/13 03:30:00", 
    "2016/10/13 04:00:00", 
    "2016/10/13 04:30:00"
]

但我有一个错误:Invalid date。你能帮我吗?

【问题讨论】:

    标签: javascript jquery date datetime


    【解决方案1】:

    使用 Array#sort 和自定义排序功能并获取最后(最大)和第一个(最小)值。

    data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
    
    var sorted = data.slice() // copy the array for keeping original array with order
      // sort by parsing them to date
      .sort(function(a, b) {
        return new Date(a) - new Date(b);
      });
    
    // get the first and last values
    console.log(
      'max :', sorted.pop(), 'min :', sorted.shift()
    );

    或者使用简单的 Array#forEach 循环。

    data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
    
    // initially set max and min as first element
    var max = data[0],
      min = data[0];
    
    // iterate over array values and update min & max
    data.forEach(function(v) {
      max = new Date(v) > new Date(max)? v: max;
      min = new Date(v) < new Date(min)? v: min;
    });
    
    console.log('max :', max, 'min :', min);

    【讨论】:

    • @user2203384 :很高兴为您提供帮助
    【解决方案2】:

    IMO,您应该使用 new Date(value)

    您的代码似乎没有任何问题。

    得到Invalid date的原因是你没有将日期转换为日期对象,你需要将它转换成JavaScript日期new Date()

    Date 对象让您可以处理日期(年、月、日、小时、分钟、秒和毫秒)

    var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
    //This will convert your date into Date object
    $.each(datesNew, function(key, value){
        dates.push(new Date(value));
    });
    var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
    var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 
    

    var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
    var dates=[];
    $.each(datesNew, function(key, value){
        //Conver date in Date object
        dates.push(new Date(value));
    });
    
    var min = new Date(Math.min.apply( null, dates));
    var max =  new Date(Math.max.apply( null, dates));
    //var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
    //var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 
    console.log(new Date(min).toString());
    console.log(new Date(max).toString());
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2022-10-13
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多