mongodb query showing datetime off by 1 hour

301 views
Skip to first unread message

shettysa...@gmail.com

unread,
Sep 6, 2018, 9:07:49 PM9/6/18
to mongodb-user
i have wriiten a query to display the date to me.
below is the code snippet
'dateFrom': {'$dateToString': {'format': '%Y-%m-%d','date': '$$list.dateFrom'}},


date in the database is stored as 07-09-2018

query displayed result as 06-09-2018

this because mongodb stores date off by 1 hour (day light saving) in db as 06-09-2018T23:00:00 and my query get me the dd,mm,yy from the datetime.

though this can be one of the solution  (courtesy Asya Kamsky)

*****************************
{$project:{date:{$dateToString:{format:"%
Y-%m-%d",date:{$add:["$created_at",X*60*60*1000]}}}}}

where X is the number of hours you are ahead of UTC (date addition in aggregation takes milliseconds).
*************************************************

is there any other way i can get he correct date i.e 07-09-2018

Thanks
Sachin



Wan Bachtiar

unread,
Oct 12, 2018, 1:53:40 AM10/12/18
to mongodb-user

is there any other way i can get he correct date i.e 07-09-2018

Hi Sachin,

It’s been a while since you posted this question, have you found the solution yet ?

Please note that you can specify timezone as an optional parameter to define a specific timezone. See $dateToString. If you don’t specify timezone the result would be displayed in UTC.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages