I have a datetimestamp object contains UTC time (string format) which I want to convert to local time (+8). I don’t know what is the correct way of doing this. Hope someone can give me an idea.
Sample
db.gps_transaction.updateMany({ imei: “861522049933584”, created_at: {$gte:“2022-04-15 00:00:00”, $lte:“2022-04-15 12:59:59”}}, [{ $set: { “datetimestamp”: {"$created_at": “$add”: [ 8 * 60 * 60 * 1000 ] } }}]);
steevej
(Steeve Juneau)
April 20, 2022, 12:35pm
2
Never store your date in local time. Store in UTC and use user’s locale setting to present in the correct format.
Never store your date as strings because every time you want to date arithmetic you need to convert $toDate anyway.
Your usage of $add is wrong. See $add documentation for examples.
Thank you @steevej but the date is already UTC - we just wanted to convert it manually to local time.
steevej
(Steeve Juneau)
April 20, 2022, 12:40pm
4
UTC but as a string. Not as a date. Do not convert to local time, string or date in your database. Always store in UTC but as date, not as string.