Why do MongoDB ISODate and Date behave differently for getDay()? - mongodb

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).

Related

MongoDb search on date field

I have a Mongo collection with 'DateAdded' in ISODate format
{
"_id" : 4098199,
"DateAdded" : ISODate("2018-08-31T05:06:13.150Z")
}
so it is 1hour off then local datetime
LocalDatetime is 2018-08-31 06:06:13 +01:00
UTC is 2018-08-31T05:06:13.150Z
Using following query when i try to get records added after 2018-08-31 06:00:00 i m not getting this records as it is in UTC
db.getCollection('User').find({"DateAdded":{$gte: new ISODate('2018-08-31T6:00:00Z')}})
How to convert UTC date while doing search on date field in MongoDB?
First of all ISODate('2018-08-31T6:00:00Z') has a typo - it should be 06:00:00Z. Without leading 0 ISODate helper returns '2018-08-31T00:00:00Z' 0am UTC and the query actually returns the document.
Secondly, there is nothing about local time. ISODate is in UTC, so
ISODate("2018-08-31T05:06:13.150Z") is 5am UTC and ISODate("2018-08-31T06:06:13.150Z") is 6am UTC. 5 < 6, so the $gte condition doesn't meet and the document is not returned.
Lastly, if you want to use local time - use Date instead:
db.getCollection('User').find({"DateAdded":{$gte: new Date('2018-08-31T6:00:00')}})
Will return all documents created after 5am UTC, 6am local time. Note, there is no Z at the end, which specifies UTC timezone.
As a recommendation - don't mess up with local time. Convert it to UTC as soon as possible and always work with UTC.

ember-cli and Rails 5 api date handling

I am trying out rails 5 api and ember-cli (2.11) for the first time. I am having trouble with date handling.
I have a date attribute in rails outputting json like
"2017-03-15"
The date (in ember inspector) becomes
Tue Mar 14 2017 20:00:00 GMT-0400 (EDT)
I use a seed file to create records like:
Planting.create(planting_type: 'seed', planting_date_begin: Date.new(2017,8,1))
I only care about date (not time). How do I get date to display correctly in Ember as well as determine if other dates are within date range (date math)?
Do I need to change my rails attribute to DateTime? If I do change to DateTime will Ember still need adjustment for timezone? What about my seed file...will I need save like a timestamp with no timezone but with time info?
Ember was converting my date into a time with wrong time zone. using the add-on https://github.com/thoughtbot/ember-utc-transform changes zone to utc resolved my issue

Mongodb faulty date [duplicate]

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

MongoDB java-driver insert date

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.

Mongodb date is off by 1 hour

I am running mongodb on ubuntu server. The server time is
root# date
Thu Sep 13 21:15:58 BST 2012
But when I run the following command I get a different result
root# mongo
MongoDB shell version: 2.2.0
connecting to: test
> new Date()
ISODate("2012-09-13T20:15:58.670Z")
There is exactly one hour difference. When I update a documents updated_on field with php using MongoDate(), the value of the field is still 1 hour off.
[EDIT]
Actually I just checked my php error log and the time in the log file is 1 hour off as well
[13-Sep-2012 20:11:14 UTC] Log Message (Time should be 21:11:14)
Mongo tells you
2012-09-13T20:15:58.670Z
Z = Zulu time / Zero offset / UTC. You can also express the time in that TZ as 2012-09-13T20:15:58.670+00:00, as defined in the ISO8601 standard by the way.
BST is UTC+1. So, they are the same time but in different time zones.
You can resolve this issue by displaying the DateTime with ToLocalTime method.
MVC C# Example: #Model.StartDate.ToLocalTime()
This is due to the way MongoDB store datetime in BST format. So the daylight savings time or the time zone of the server will have an effect on the actual date time returned to the application. This simple code will be able to format as usual with ToString("dd MMMM yyyy hh:mm tt") or any other format based on your requirements.
Here you need to understand a concept in time setting in clocks called daylight saving time. In some countries around the world the clock is advanced by 1 or more hours to experience day light by one more hour. The difference between IST and GST is 5.30 hrs but the actual time difference is between New Delhi and London time is 6.30 hrs. See this article from 4GuysFromRolla for setting and using server time.
On windows change your timezone.
Controll Panel -> Date and Time -> Change on timezone -> (UTC) Coordinated universal time.
And then just change your time