0

I have a d3 function (below) to group data that looks like this:

[{"count":17,"created_at":"2012-08-20T04:10:08Z","id":1,"notes":"","occurred_at":"2012-08-19T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:10:08Z"},{"count":25,"created_at":"2012-08-20T04:11:06Z","id":4,"notes":"","occurred_at":"2012-08-18T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:11:06Z"},{"count":40,"created_at":"2012-08-20T21:05:47Z","id":94,"notes":null,"occurred_at":"2012-07-16T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T21:05:47Z"},{"count":10,"created_at":"2012-08-20T04:13:38Z","id":8,"notes":"","occurred_at":"2012-08-17T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T04:13:38Z"},{"count":9,"created_at":"2012-08-20T20:35:19Z","id":11,"notes":null,"occurred_at":"2012-08-13T13:30:00Z","seizure_type_id":1,"updated_at":"2012-08-20T20:35:19Z"}]

This appears to produce the correct result, however I'd like to offset the parsed dates with my timezone (EST currently) so that the groupings are for the current timezone not UTC. Also if anyone knows of a better way to group my data, I'd love to see it.

  @group_data: (inputData) ->
    format = d3.time.format.iso
    gformat = d3.time.format("%Y-%m-%d")
    array = d3.nest()
      .key((d) ->
        gformat(format.parse(d.occurred_at))
      )
      .entries(inputData)
      .map((d) ->
        group = d.key
        total = 0
        d.values.forEach((day) ->
          total = total + day.count
        )
        return {'group': group, total:total }
      )
    obj = {}
    array.forEach((d) -> obj[d.group] = d.total)
    obj
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • You don't seem to have any timezone information attached to the dates in your data, so it should be parsed into your local timezone. Could you post a complete working example that demonstrates the problem please? – Lars Kotthoff Dec 27 '13 at 19:15
  • The occurred_at dates are in UTC (all dates are converted to UTC when saving to the DB). I know that for this user their timezone is EST so I'd simply like to adjust the datetime 2012-08-20T04:10:08Z to 2012-08-19T11:10:08 EST. I obviously can just subtract 5 hours from each of the datetime entries, but was wondering if there was a better way – rainkinz Dec 27 '13 at 19:25
  • [This question](http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) may help. – Lars Kotthoff Dec 27 '13 at 21:41
  • @LarsKotthoff, brilliant thanks https://github.com/mde/timezone-js looks like the answer. Make it an answer if you want and I'll accept it. By the way, did my use of d3.nest look correct? – rainkinz Dec 27 '13 at 23:25
  • Done, thanks. Your use of `d3.nest` looks fine to me -- as long as you're achieving the nesting you're looking for, it should be fine. – Lars Kotthoff Dec 28 '13 at 08:19

1 Answers1

0

You can use timezone-js to convert dates between time zones.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204