I'm using MongoDB 2.2 with java-driver 2.10.1
I'm inserting a date field into a document from a java.util.Date instance. My instance has the following value:
Wed Oct 10 00:00:00 CEST 2012
but once in mongo, I have this value:
ISODate("2012-10-09T22:00:00Z")
My insertion code:
BasicDBObject doc = new BasicDBObject("key", event.getKey())
.append("title", event.getTitle())
.append("description", event.getDescription())
.append("date", event.getDate());
db.getCollection("events").insert(doc);
You can have a look to the date instance referenced from my event object on this debug screenshot:
Is there something to do with the timezone ? Or a bug from the driver ?
Dates in MongoDB are always stored as UTC datetimes, so what you're seeing is correct.
The CEST time zone is two hours ahead of UTC (GMT) so your time's correct UTC representation is two hours earlier than your CEST time, which is exactly what you're seeing.
Related
When I look at all the documents in Compass, I can see all the dates are UTC as expected. But when I used Mongo-Java-Driver and read from the db using MongoCollection -> MongoCursor - all the Date objects in my Document object is in my local timezone (PDT)
Does anyone know how to keep the date in UTC format when I do my read
I am storing dates in Mongo DB using Mongoose, they should be in UTC, but I am seeing this using a Mongo IDE (Studio3T) - I take it that the +0000 does not mean UTC?
"eventDate" : ISODate("2019-12-27T17:01:00.000+0000"),
+0000 means UTC according to ISO 8601. The letter Z is also possible to indicate UTC time.
Yes,Dude! By default the dates are stored in UTC format in MongoDB.
For further information,head over to below links.
https://docs.mongodb.com/manual/tutorial/model-time-data/
MongoDb BSON stores Date in UTC time
When getting some records, I'm seeing that some dates are showing as GMT-0400 (EDT) and others are GMT-0500 (EST).
Dates are being added in mongoose simply by using Date.now in the schema.
Any ideas what could cause the offsets to be different?
Edit: Here's an example:
Stored as: ISODate("2015-10-30T15:36:47.287Z") Returned as: Fri Oct 30 2015 11:36:47 GMT-0400 (EDT) using find() with Mongoose
Stored as: ISODate("2015-11-07T14:44:47.956Z") Returned as: Sat Nov 07 2015 09:44:47 GMT-0500 (EST) using find() with Mongoose
It's just the default string representation of the underlying UTC timestamp, showing the timestamp in the local time zone that was in effect at the time.
In 2015, daylight saving time ended at 2:00 AM on Nov 1, so that's why the first one is shown in EDT and the second one is shown in EST.
Any queries you do are always performed using UTC time.
Try this in a Mongo 2.6.4 shell:
> tues = ISODate("2014-07-01")
ISODate("2014-07-01T00:00:00Z")
> tues.getDay()
1
> tues = new Date(2014, 6, 1)
ISODate("2014-07-01T04:00:00Z")
> tues.getDay()
2
i.e. create an ISODate for July 1, 2014 (a Tuesday), and run getDay() to get the day of the week and get 1, then create the same date with a standard Javascript Date and, as expected with Javascript, getDay() returns 2.
From the Mongo docs, it seems ISODate is simply a wrapper for Date. I can't find any documentation saying it behaves differently for getDay(). Is this intended behavior? If so, why? A bug?
getDay is giving you the day based on the local timezone. I am suspecting you are in U.S. EDT timezone. So in the first case, your local time is still 2014-06-30 20:00:00 EDT (Mon).
When I get the date object and print it in mongo shell, it display two different time as follows:
>new Date()
Mon Feb 06 2012 18:49:40 GMT+0530 (IST)
>printjson({created_at: new Date()})
{ created_at : ISODate("2012-02-06T13:19:40.313Z") }
The two times are different, what i am wrong.
This is because of mongodb always store dates in UTC format, but javascript show your local time. And printjson internal mongodb shell function that convert date from your local timezone to utc format. So it shows -5.30 hours backward from your current time.
The times are not different, they are exactly the same! It's merely the same time expressed in different timezones. The "Z" in ISODate means "UTC" (or as some people try to call it: GMT). You're on IST (Indian Standard Time I guess) which is at GMT+0530. 18:49:40 # GMT+5:30 is exactly the same as 13:19 # GMT.
When I run the same code you shown on the shell, I get:
> new Date()
ISODate("2012-02-06T13:34:10.667Z")
As you can see, that is also with "Z". Perhaps you're running an older version of the shell?
cheers,
Derick