简体   繁体   English

如何在javascript中按小时/6小时/天/周/月/年对日期对象进行分组

[英]How to group date object by hour/6 hours /day/week/month/year in javascript

I Have 10 years of records.我有10年的记录。 so API will return the array I need to group the array based on date time stamp.所以 API 将返回我需要根据日期时间戳对数组进行分组的数组。 A user can choose a grouping option as用户可以选择一个分组选项作为

  • hour小时
  • 6 hours(per day) 6小时(每天)
  • day
  • week星期
  • month
  • year

data example数据示例

[{
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.18809978382007495
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.42347719862654404
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.365386421616013
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.49364744650351033
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.31133576985952016
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5509062071104307
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5556739085429424
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6668348570127745
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.4908101365372941
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6042127351503115
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.3725497529313371
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.29687095332790064
}, {
  "x": "2018-02-07T11:19:19.034Z",
  "y": 0
}]

Is there any generic solutions to get grouping by hour/day/week/month/year是否有任何通用的解决方案可以按小时/天/周/月/年进行分组

Example output what I want.示例输出我想要的。

for hour小时

{
 "OCT 2th 2012 6AM": [
    "2012-10-02 06:00:00",
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

for every 6 hour每 6 小时

{
 "OCT 6th 2012 first 6 hours": [
    "2012-10-11 06:00:00",
  ],
 "OCT 6th 2012 second 6 hours": [
    "2012-10-10 06:03:51"
  ],
  "NOV 6th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-111-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

for day一天

{
 "OCT 2th 2012": [
    "2012-10-2 06:00:00",
  ],
 "OCT 10th  2012": [
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 7th 2013": [
    "2013-10-07 02:07:02"
  ],
}

.for week .一周

{
 "OCT 2012 week number": [
    "2012-10-2 06:00:00",
  ],
 "OCT 2012 week number": [
    "2012-10-10 06:03:51"
  ],

}

for month一个月

{
 "OCT 2012 ": [
    "2012-10-2 06:00:00",
  ],
 "NOV 2012": [
    "2012-11-10 06:03:51"
  ],

}

You can do it the following way maybe?你可以通过以下方式做到这一点吗?

 const data = [{"x":"2019-02-05T14:28:45.540Z","y":0.18809978382007495},{"x":"2019-02-05T14:28:45.540Z","y":0.42347719862654404},{"x":"2019-02-05T14:28:45.540Z","y":0.365386421616013},{"x":"2019-02-05T14:28:45.540Z","y":0.49364744650351033},{"x":"2019-02-05T14:28:45.540Z","y":0.31133576985952016},{"x":"2019-02-05T14:28:45.540Z","y":0.5509062071104307},{"x":"2019-02-05T14:28:45.540Z","y":0.5556739085429424},{"x":"2019-02-05T14:28:45.540Z","y":0.6668348570127745},{"x":"2019-02-05T14:28:45.540Z","y":0.4908101365372941},{"x":"2019-02-05T14:28:45.540Z","y":0.6042127351503115},{"x":"2019-02-05T14:28:45.540Z","y":0.3725497529313371},{"x":"2019-02-05T14:28:45.540Z","y":0.29687095332790064},{"x":"2018-02-07T11:19:19.034Z","y":0}]; const groups = (() => { const byDay = (item) => moment(item.x).format('MMM DD YYYY'), forHour = (item) => byDay(item) + ' ' + moment(item.x).format('hh a'), by6Hour = (item) => { const m = moment(item.x); return byDay(item) + ' ' + ['first', 'second', 'third', 'fourth'][Number(m.format('k')) % 6] + ' 6 hours'; }, forMonth = (item) => moment(item.x).format('MMM YYYY'), forWeek = (item) => forMonth(item) + ' ' + moment(item.x).format('ww'); return { byDay, forHour, by6Hour, forMonth, forWeek, }; })(); const currentGroup = 'forWeek'; console.log(_.groupBy(data, groups[currentGroup]));
 <script src="https://lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script> <script src="https://momentjs.com/static/js/global.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按天、1 小时、6 小时、8 小时、周、月和年对日期数组和总和值进行分组? - How to group array of dates and sum value by day, 1hour, 6hours, 8hours, week, month, and year? Javascript、时间和日期:获取给定毫秒时间的当前分钟、小时、日、周、月、年 - Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond time JavaScript:将日期格式化为小时:分钟:第二天月日年 - JavaScript: format date to hour:minute:second day month day year Javascript对象组按天,月,年 - Javascript object group by day, month, year Javascript对象组按天,月,年 - Javascript object group by day,month,year 如何在给定日期 - 月份和日期的情况下返回正确的星期几 - How to return the correct day of the week given a date - year month and day Javascript,时间和日期:获取下一天,周,月,年等 - Javascript, Time and Date: Getting the next day, week, month, year, etc 包含按星期几的商店营业时间的组对象 - Group object that contains store hours by day of week 通过日/月/年JavaScript创建新的Date对象时出错? - A bug in creating a new Date object from day/month/year JavaScript? 从 JavaScript 中其他语言环境的年、月、日获取日期 object - obtain date object from year, month and day in other locale in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM