When I create a date in javascript it is in zulu, when I save it in my mongodb via mongoose it is transformed to UTC (its keeps the same time value, but the timezone is changed). I'd like it to stay in zulu when saved, how can I set this option in mongoose ?
thanks
MongoDB stores all DateTimes in UTC. Any local times you supply are converted to UTC when stored in the database. The recommended approach is to always convert DateTime values to UTC yourself before storing them in the database, that way you are in full control.
Resources:
https://jira.mongodb.org/browse/CSHARP-185
Dealing with how MongoDB stores DateTime when used with Service Locator Pattern
Related
I'm trying to get the Timestamp value from firestore (using Firebase Functions), and I´ve successfully done it localy with the toDate() method of Timestamp, and moment library.
moment(doc.data().EndDate.toDate())
But when I deploy my code to firebase and test the function, somehow the toDate() returns a Date with 1 less hour than the saved timestamp on firebase. I suppose it is transforming my date to UTC, since I'm in UTC+1, and the Timestamp is also stored with UTC+1 in firestore, but I don't know how to reliably get the timestamp date as is in firestore, regardless of timezones.
If someone knows why this happens or has any idea how to solve it it would be great.
All timestamps in Firestore are stored in UTC. If you see something different in the Firebase console, that's just your browser formatting it for your local timezone.
In JavaScript, all Date objects are also represented in UTC. If you format that as a string, you will again possibly get a different representation based on your local timezone.
If you write code that computes values using dates or timestamps, you should perform all your computations using UTC. This is the pretty much all computing systems want to deal with dates. When it comes time to format the date for display to an end user, only then should you take timezone into account, and present something according to the user's preferences.
I want to insert an item to the DB in a specific date based on the client's timezone. So if client desired in today on 20:00, and his timezone is +3, I want that node and mongodb will set the date to be on today on 17:00. I hope that is clear enough.
Generally it is recommended to save all timestamps in database in UTC only, along with it you can save extra field timezone/offset value in each document. Check -
https://docs.mongodb.com/manual/tutorial/model-time-data/
But if you want to save in local timezone, you need to do conversion at application level before inserting in database.
I'm trying to insert start and end time like entry and exit time logic into Mongodb from node js.
My local system's timezone is IST. +5:30 offset for both client and sever.
So now follow this example.
entryDate: 15-10-2018,
start: 15-10-2018 01:00:00,
end: 15-10-2018 05:00:00
Now since i'm on same timezone on client and server, it won't change my date value and create new object.
Then i'm storing this data into MongoDB. As you know that mongo db will convert date to UTC and then stores it. So in my DB it will look like this.
entryDate: ISODate("2018-10-15T00:00:00Z"),
start: ISODate("2018-10-14T19:30:26Z"),
end: ISODate("2018-10-14T23:30:26Z")
So now if I want to search a query that should give me records for dates and time between 15 to 16 let's say then i won't get any data.
Now consider this case. I moved app on to the server. Now servers timezone is in UTC. So every date I pass will be converted directly in UTC so it'll insert same data like before.
Now difference here is when i want to search date range in IST timezone then it will give me the exact date to search like (15-10-2018 00:00:00) to (16-10-2018 00:00:00).
Where on server it will convert this date to UTC and can give me one day less or may be higer time like (14-10-2018 18:30:00) to (15-10-2018 18:30:00). So from the start my query is wrong.
So how to solve this issue? Thank you.
After doing some research. I have a solution.
Get your date on the client side.
Convert that date to UTC date and pass date string to the server. Like 01-01-2018 10:00:00. (Note: we are just converting date, not the timezone)
On the server side create a date object from the provided date that will convert the date to UTC by default with that same time.
Store the date on the server side with MongoDB. Since the date is in UTC it won't convert the date again and store it as it is.
Now from the server, you will get the date in UTC format which will be like '2018-01-01 10:00:00'.
Now on the client side, create a date object from this date. This will convert your date to actual date that you passed.
This solved my problem.
Am using:
mongoDB to fetch data to the server which is based on: node.js and express. Template engine is PUG aka. Jade.
in each document stored inside the DB there is a date attribute stored as UTC.
Question/problem.
I need to convert UTC to the clients local-time.
Solving alternatives:
Using Aggregation in MongoDB
Using moment.js library and make the conversion in node
Pass moment.js object inside the view and make the conversion their,
as suggested in this post:
How can I format a date coming from MongoDB?
Using moment.js to format an ISO 8601 date string to local time can be achieved like this:
let moment = require('moment');
let localizedDate = moment('2014-06-01T12:00:00Z').format('LT');
console.log(localizedDate);
Here I got an input box on my web page for users to input a date. For some reason I can't use a datetime picker and I have to pass it to an .NET based service as a String via ajax.
Users may from different time zones. And the date is stored as UTC in database.
It seems I have 2 options to handle the timezone:
Convert the date string to UTC date string at frontend and pass to service.
Pass the UTC offset to service and convert the date string to UTC at backend.
However, neither option can handle the daylight savings time.
Could anyone give me some suggestions on this?
Javascript's toUTCString() and functions like getUTCDate() instead of getDate() will ignore the timezone offset, including I presume the DST offsets.