• <bdo id='SEZbx'></bdo><ul id='SEZbx'></ul>

    <small id='SEZbx'></small><noframes id='SEZbx'>

        <legend id='SEZbx'><style id='SEZbx'><dir id='SEZbx'><q id='SEZbx'></q></dir></style></legend>
        <tfoot id='SEZbx'></tfoot>
        <i id='SEZbx'><tr id='SEZbx'><dt id='SEZbx'><q id='SEZbx'><span id='SEZbx'><b id='SEZbx'><form id='SEZbx'><ins id='SEZbx'></ins><ul id='SEZbx'></ul><sub id='SEZbx'></sub></form><legend id='SEZbx'></legend><bdo id='SEZbx'><pre id='SEZbx'><center id='SEZbx'></center></pre></bdo></b><th id='SEZbx'></th></span></q></dt></tr></i><div id='SEZbx'><tfoot id='SEZbx'></tfoot><dl id='SEZbx'><fieldset id='SEZbx'></fieldset></dl></div>
      1. 如何按天、周、月对具有时间戳属性的对象进行

        时间:2023-08-07
        <tfoot id='XqXv7'></tfoot>

            • <bdo id='XqXv7'></bdo><ul id='XqXv7'></ul>

              • <legend id='XqXv7'><style id='XqXv7'><dir id='XqXv7'><q id='XqXv7'></q></dir></style></legend>
                  <tbody id='XqXv7'></tbody>

                  <small id='XqXv7'></small><noframes id='XqXv7'>

                  <i id='XqXv7'><tr id='XqXv7'><dt id='XqXv7'><q id='XqXv7'><span id='XqXv7'><b id='XqXv7'><form id='XqXv7'><ins id='XqXv7'></ins><ul id='XqXv7'></ul><sub id='XqXv7'></sub></form><legend id='XqXv7'></legend><bdo id='XqXv7'><pre id='XqXv7'><center id='XqXv7'></center></pre></bdo></b><th id='XqXv7'></th></span></q></dt></tr></i><div id='XqXv7'><tfoot id='XqXv7'></tfoot><dl id='XqXv7'><fieldset id='XqXv7'></fieldset></dl></div>
                  本文介绍了如何按天、周、月对具有时间戳属性的对象进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 nodejs 应用程序中,我有一个事件对象数组,格式如下:

                  In a nodejs application, I have an array of event objects formatted as follows:

                  eventsArray = [ {id: 1, date: 1387271989749 }, {id:2, date: 1387271989760}, ... ]
                  

                  eventsArray 具有可变长度的 n 个元素,假设我选择时间参考为巴黎时间,我希望能够按天、周或月对元素进行分组:

                  eventsArray having a variable length of n elements, and supposing I choose time reference to be Paris time, I want to be able to group elements by day, week, or month:

                  groupedByDay = {
                  
                              2012: { ... },
                              2013: {
                                dayN  : [{id: a0, date: b0}, {id: a1, date: b1}, {id: a2, date: b2 }],
                                dayN+1: [{id: a3, date: b3}, {id: a4, date: b4}, {id: a5, date: b5 }],
                                ...
                                     },
                              2014: { ... }
                  
                            }
                  
                  groupedByWeek = {
                              2012: { ... }
                              2013: {
                                weekN: [{id: a0, date: b0}, {id: a1, date: b1}, {id: a2, date: b2 }],
                                weekN+1: [{id: a3, date: b3}],
                                ....
                                    },
                               2014: { ... }
                                  }
                  
                  groupedByMonth = {
                               2012: { ... },
                               2013: {
                                 monthN: [ {id: a0, date: b0 }, {id: a1, b1}, {id: a2, b2}, {id: a3, b3 }],
                                 monthN+1: [ {id: a4, date: b4 }, {id: a5, b5}, {id: a6, b6}],
                                 ...
                                     },
                                2014: { ... }
                                   }
                  

                  在处理 unix 时间戳方面几乎没有经验,我想知道如何做到这一点,或者是否有一个 npm 模块可以让这更容易.

                  Having very little experience with manipulating unix timestamps, I was wondering how this could be done or if there was an npm module that would make this easier.

                  推荐答案

                  这是我的解决方案.请记住,日、周和月是相对于起源而言的,因为纪元:

                  Here is my solution. Keep in mind that day, week and month are relative to origin, since epoch:

                  let event = [ {id: 1, date: 1387271989749 }, {id:2, date: 1387271989760} ];
                  
                  function groupday(value, index, array){
                     let byday={};
                      let d = new Date(value['date']);
                      d = Math.floor(d.getTime()/(1000*60*60*24));
                      byday[d]=byday[d]||[];
                      byday[d].push(value);
                    return byday
                  }
                  
                  function groupweek(value, index, array){
                    let byweek={};
                     let d = new Date(value['date']);
                      d = Math.floor(d.getTime()/(1000*60*60*24*7));
                      byweek[d]=byweek[d]||[];
                      byweek[d].push(value);
                    return byweek
                  }
                  
                  function groupmonth(value, index, array){
                     let bymonth={};
                      d = new Date(value['date']);
                      d = (d.getFullYear()-1970)*12 + d.getMonth();
                      bymonth[d]=bymonth[d]||[];
                      bymonth[d].push(value);
                    return bymonth
                  }
                  
                  console.log('grouped by day', event.map(groupday));
                  console.log('grouped by week',event.map(groupweek));
                  console.log('grouped by month',event.map(groupmonth));
                  
                  
                  

                  注意:对象是分组的,但日、周和月的键是数字的.只有年份的键是人类可读的.

                  NOTE: Objects are grouped but the keys for day, week and month are in digits. Only the key for year is human readable.

                  这篇关于如何按天、周、月对具有时间戳属性的对象进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Javascript比较两个日期以获得差异 下一篇:将日期转换为时间戳,以便在 javascript 中存储到

                  相关文章

                  最新文章

                  1. <small id='KB5Iq'></small><noframes id='KB5Iq'>

                    1. <legend id='KB5Iq'><style id='KB5Iq'><dir id='KB5Iq'><q id='KB5Iq'></q></dir></style></legend>
                      <i id='KB5Iq'><tr id='KB5Iq'><dt id='KB5Iq'><q id='KB5Iq'><span id='KB5Iq'><b id='KB5Iq'><form id='KB5Iq'><ins id='KB5Iq'></ins><ul id='KB5Iq'></ul><sub id='KB5Iq'></sub></form><legend id='KB5Iq'></legend><bdo id='KB5Iq'><pre id='KB5Iq'><center id='KB5Iq'></center></pre></bdo></b><th id='KB5Iq'></th></span></q></dt></tr></i><div id='KB5Iq'><tfoot id='KB5Iq'></tfoot><dl id='KB5Iq'><fieldset id='KB5Iq'></fieldset></dl></div>
                    2. <tfoot id='KB5Iq'></tfoot>
                      • <bdo id='KB5Iq'></bdo><ul id='KB5Iq'></ul>