Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Javascript中的静态日期数组创建日期间隔数组?_Javascript_Date - Fatal编程技术网

如何从Javascript中的静态日期数组创建日期间隔数组?

如何从Javascript中的静态日期数组创建日期间隔数组?,javascript,date,Javascript,Date,从阵列中: ["01/01/2015", "02/01/2015", "03/01/2015", "05/01/2015", "06/01/2015"]; 到 是否有可用于此类型函数的库 编辑更多信息:我想将第一个静态日期数组转换为表示时间间隔的对象数组 我处理力矩对象和下划线。我最终得到了以下工作代码: //Create arrays var coll_dateIntervals = []; var arr_temp = [];

从阵列中:

["01/01/2015", "02/01/2015", "03/01/2015", "05/01/2015", "06/01/2015"]; 

是否有可用于此类型函数的库


编辑更多信息:我想将第一个静态日期数组转换为表示时间间隔的对象数组

我处理力矩对象和下划线。我最终得到了以下工作代码:

        //Create arrays 
        var coll_dateIntervals = []; 
        var arr_temp = []; 
        _.each(collDates, function(moment_date, index, list){ 
            //Day difference in # of days 
            var diff = Math.abs(collDates[index].diff(collDates[index + 1], "days")); 

            //Add begin_date 
            arr_temp.push(moment_date); 
            //Check the date difference in days. 
            if(diff <= 1 && diff !== undefined){ 
                //If the difference is 1, than add the date to the temporary array 
                arr_temp.push(moment_date); 
                //If it's more than 1 day, or the last object 
            } else if (diff > 1 || diff === undefined){ 
                //Store the interval in an object 
                var obj_dateInterval = { start_date: arr_temp[0].format("DD/MM/YYYY"), end_date: arr_temp[arr_temp.length - 1].format("DD/MM/YYYY")}; 
                coll_dateIntervals.push(obj_dateInterval); 

                //Empty the array to start the new loop 
                arr_temp = []; 
            }; 
        }); 
        console.log(coll_dateIntervals); 

        //return coll_dateIntervals; 
//创建数组
var coll_dateinterval=[];
var arr_temp=[];
_.each(collDates,function(moment_date,index,list){
//以天为单位的日差
var diff=Math.abs(collDates[index].diff(collDates[index+1],“days”);
//添加开始日期
到达温度推送(时刻/日期);
//检查日期差异(以天为单位)。

如果(不同的是,看起来您正在尝试构建一个JSON字符串,对吗?是的。我在后端的表结构应该是最好的开始日期和结束日期格式。为什么会有否决票?这不是一个有效的编程问题?看起来像是一个有用的库,但还没有在Google上找到它……您尝试进行转换的逻辑是什么?我看不到第一个数组和第二个数组之间的连接。似乎您试图将奇数数量的数据放入需要偶数数量数据的格式中。在数组中,哪些字符串是开始日期,哪些是结束日期?
        //Create arrays 
        var coll_dateIntervals = []; 
        var arr_temp = []; 
        _.each(collDates, function(moment_date, index, list){ 
            //Day difference in # of days 
            var diff = Math.abs(collDates[index].diff(collDates[index + 1], "days")); 

            //Add begin_date 
            arr_temp.push(moment_date); 
            //Check the date difference in days. 
            if(diff <= 1 && diff !== undefined){ 
                //If the difference is 1, than add the date to the temporary array 
                arr_temp.push(moment_date); 
                //If it's more than 1 day, or the last object 
            } else if (diff > 1 || diff === undefined){ 
                //Store the interval in an object 
                var obj_dateInterval = { start_date: arr_temp[0].format("DD/MM/YYYY"), end_date: arr_temp[arr_temp.length - 1].format("DD/MM/YYYY")}; 
                coll_dateIntervals.push(obj_dateInterval); 

                //Empty the array to start the new loop 
                arr_temp = []; 
            }; 
        }); 
        console.log(coll_dateIntervals); 

        //return coll_dateIntervals;