How to aggregate by year-month-day on a different timezone

后端 未结 8 1653
无人及你
无人及你 2020-11-29 02:21

I have a MongoDB whom store the date objects in UTC. Well, I want to perform aggregation by year,month day in a different timezone (CET).

doing this, works fine for

相关标签:
8条回答
  • 2020-11-29 03:08
    <?php
        date_default_timezone_set('Asia/Karachi');
        $date=getdate(date("U"));
        $day = $date['mday'];
        $month =$date['mon'];
        $year = $date['year'];
        $currentDate = $year.'-'.$month.'-'.$day;
    ?>
    
    0 讨论(0)
  • 2020-11-29 03:10

    Use for example moment.js to dertmine the current timezone offset for CET but this way you get the summer&winter offsets

    var offsetCETmillisec = moment.tz.zone('Europe/Berlin').offset(moment())* 60 * 1000;
    
      $group: {
        _id: {
          'year': {'$year': [{ $subtract: [ '$createdAt', offsetCETmillisec ]}] },
          'month': {'$month': [{ $subtract: [ '$createdAt', offsetCETmillisec ]}] },
          'day': {'$dayOfMonth': [{ $subtract: [ '$createdAt', offsetCETmillisec ]}] }
        },
        count: {$sum: 1}
      }
    }
    
    0 讨论(0)
提交回复
热议问题