如何在jquery中查找日期对象的最大值和最小值? [英] How to find max and min value of date object in jquery?

查看:117
本文介绍了如何在jquery中查找日期对象的最大值和最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

Hii朋友,



我用以下代码查找min和使用Jquery从日期数组中获取日期的最大值。



var日期[

日期{Wed Jun  08   2016   00  00  00  GMT + 0530(印度标准时间)},日期{Thu Jun  09  < span class =code-digit> 2016   00  00  00  GMT + 0530(印度标准时间)},日期{Fri Jun  10   2016   00  00  00  GMT + 0530(印度标准时间)} 



];

var minDate;

mindate = new日期(Math.min.apply(null,dates));



但它返回无效日期。



请帮助。


我的尝试:



var日期[

日期{Wed Jun  08   2016   00  00  00  GMT + 0530(印度标准时间)},日期{Thu Jun  09   2016   00  00  00  GMT + 0530(印度标准时间)},日期{Fri Jun  10   2016   00  00  00  GMT + 0530(印度标准时间)} 



];

var minDate;

mindate = new Date(Math.min.apply(null,dates));

解决方案

Json数组转换为日期数组,然后应用 Math.min。申请功能



例如:



  var  date1 =  new  日期() ; 
date1.setDate(date1.getDate()+ 1 );
var date2 = new 日期();
date2.setDate(date2.getDate()+ 2 );
var date3 = new 日期();
date3.setDate(date3.getDate()+ 3 );
var date4 = new 日期();
date4.setDate(date4.getDate() - 5 );
var _Dates = [{date:date1},{date:date2},{date:date3},{date:date4}];


// 将json数组中的值添加到日期数组中。
var dateArray = [];
for var i = 0 ; i< _Dates.length; i ++){
dateArray.push(_Dates [i] .date);
}
var minDate = new 日期 Math .min.apply( null ,dateArray));


请参阅我对该问题的评论。即使问题的表述也应该用JavaScript编写。例如:

  var  points = [
new 日期 2016 0 ),
new 日期 2018 1 ),
new 日期 1999 1 ),
new 日期 302 0 )];



请参阅:日期 - JavaScript



如何比较时间点?您只需使用运算符'<'和'>'为日期对象定义的事实。



计算最小值和最大值的函数可以,例如,返回属性 min max

  function  getMinMaxTime(points){
if (!points) return undefined ;
if (!points.length) return 未定义; // not array
if (points.length< 0 return null < /跨度>;
var min = points [ 0 ];
var max = points [ 0 ];
for var index in 分){
if (points [index]> max)max = points [index];
if (points [index]< min)min = points [index];
}
return {min:min,max:max};
}





没有数组或作为参数传递的对象的病态情况的返回不是数组或没有点只是例如。您可能决定不进行检查,然后处理可能的异常,这也是一个合理的决定。



-SA

Hii friends,

I have used following code to find min and max value of date from an array of date using Jquery.

var Dates[

Date {Wed Jun 08 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Thu Jun 09 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Fri Jun 10 2016 00:00:00 GMT+0530 (India Standard Time)}


];
var minDate;
mindate = new Date(Math.min.apply(null, dates));

But it returns invalid Date.

please help.

What I have tried:

var Dates[

Date {Wed Jun 08 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Thu Jun 09 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Fri Jun 10 2016 00:00:00 GMT+0530 (India Standard Time)}


];
var minDate;
mindate = new Date(Math.min.apply(null, dates));

解决方案

convert the Json array into a date array and then apply the Math.min.apply function

example:

var date1 = new Date();
       date1.setDate(date1.getDate() + 1);
       var date2 = new Date();
       date2.setDate(date2.getDate() + 2);
       var date3 = new Date();
       date3.setDate(date3.getDate() + 3);
       var date4 = new Date();
       date4.setDate(date4.getDate() - 5);
       var _Dates = [{ date: date1 }, { date: date2 }, { date: date3 }, { date: date4 }];


      // adding the values from json array into a date array.
       var dateArray = [];
       for (var i = 0; i < _Dates.length; i++) {
           dateArray.push(_Dates[i].date);
       }
       var minDate = new Date(Math.min.apply(null, dateArray));


Please see my comment to the question. Even the formulation of the problem should be written in JavaScript. For example:

var points = [
   new Date(2016, 0),
   new Date(2018, 1),
   new Date(1999, 1),
   new Date(302, 0)];


Please see: Date — JavaScript.

How to compare the points of time? You simply can use the fact the the operators '<' and '>' are defined for the Date objects.

The function to calculate both minimum and maximum can, for example, return a stricture with the properties min and max:

function getMinMaxTime(points) {
    if (!points) return undefined;
    if (!points.length) return undefined; // not array
    if (points.length < 0) return null;
    var min = points[0];
    var max = points[0];
    for (var index in points) {
        if (points[index] > max) max = points[index];
        if (points[index] < min) min = points[index];
    }
    return { min: min, max: max };	
}



The return for pathological cases when there is no array or the object passed as the argument is not an array or there are no points are just for example. You may decide to do no check-ups, and then deal with possible exceptions, which is also would be a sound decision.

—SA


这篇关于如何在jquery中查找日期对象的最大值和最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆