I have a Docker container with 'Debian GNU/Linux 8 (jessie)' and node.js 8. The system timezone (in /etc/timezone) is 'Europe/Moscow' but when I run node and type
new Date();
I get a UTC date, but not a local Moscow date.
How to make node.js use system timezone by default?
Node.js is using your system's timezone, use the following to check:
new Date().toString()
When you do: new Date(), the output you get is in UTC, this differs from the output you get in Chrome's console, where new Date().toString() & new Date() gives the same string format. But that doesn't mean it's not using your system timezone. You're getting your current time, converted to UTC
console.log(new Date())
To confirm set a particular date:
console.log(new Date(2019,11,11,15,31,0).toString())
// Wed Dec 11 2019 15:31:00 {YOUR-TIMEZONE}
Related
Without using momentjs on client-side. I need to properly save the date/time to the db and pull it back, followed by converting it to local.
I noticed the material-ui datepicker took care of this before but now I need to do it myself.
The html input 'datetime-local' doesn't like receiving timezone so I'm not sure how to do the whole flow.
I think I need to get UTC date on the client side like this for the db (?)
2021-10-26T01:14:27.920+00:00
And somehow on turn it into something input 'datetime-local' will accept.
Any help appreciated
You can achive in the folllowing ways
// Create date object from datetime string
var date = new Date('2021-10-26T01:14:27.920+00:00');
// Coverting local datetime back to UTC
console.log(date.toUTCString()); // Output "Tue, 26 Oct 2021 01:14:27 GMT"
// Coverting to local datetime
console.log(date.toString()); // Output "Tue Oct 26 2021 06:44:27 GMT+0530 (India Standard Time)"
// Coverting to local timezone
console.log(date.toLocaleString()); // Output "10/26/2021, 6:44:27 AM"
Can anyone tell me which is the right format to store date in my database.
Suppose if the client and the server are in the same region, we can use unix epoch timestamp or date string. But if the server is in US and the client is from Asia, I knew people saying UTC to auto convert date from the particular region. But I cannot store UTC in MongoDB via Node js, maybe i made it in the wrong way.
.currently I store "09-02-2021" for date and 1612837056077 timestamp. I also get the time from the front end.Also, I am not sure with the above things. Can someone explain the right way of using date and time in a real world application. If possible show me a quick demo in node js and mongoose
Thank you all :)
UTC is the way to go. I don't know what you mean by "But I cannot store UTC in MongoDB via Node js". Create your schema like so:
const client_schema = new mongoose.Schema({
...
...
date_created: {type: Date, default: Date.now},
...
...
});
And date_created will always be stored in UTC format. From UTC, you can convert it to any timezone. Here is just one example, using moment, of how to convert UTC to a Eastern Timezone:
const d = moment(utc_date).utcOffset(moment(utc_date).tz("America/New_York").format('Z'), true).toDate();
Note: moment is being phased out and is no longer recommended for new projects.
just wondering why and how when I save some date() in mongoose model:
renovate: {
type: Date
}
it is storage in mongodb as:
ISODate("2020-04-12T11:54:54.568Z"
but if I fetch this data into my front end looks like current (correct) time?
Sun Apr 12 2020 13:54:54 GMT+0200 (Central European Summer Time)
MongoDB can only store times in UTC. Mongoose appears to translate UTC timestamps into local time, presumably for convenience.
I am using flatpickr in order to set a date,
onChange: function(selectedDates, dateStr, instance) {
var myDate = selectedDates[0];
First I am tracing selecteDate[0] and gives me a string
Thu Dec 20 2018 00:00:00 GMT-0500 (GMT-05:00)
Then I am tracing myDate and seems that is been converted to GMT+0000 ISO string.
2018-12-20T05:00:00.000Z
So my question is where does the flatpickr picks the GMT-0500 from?
Is it possible to change it?
Unfortunately, it doesn't seem to be possible to do that (currently).
https://github.com/flatpickr/flatpickr/issues/1532
Apparently, flatpickr uses, by default, the timezone of the computer.
My users could create some objects that have specific time and search them by query for specific startTime and endTime.
My question is how should I store these objects in my database (MongoDB)? Should I store them in UTC or with specific timezone that relates to user that create object?
Use UTC date format, and convert all data to UTC: when create, update, delete objects or filter them. Use one UTC time zone helps you avoid a lot of problems, and simplify you code.
example
> var now = new Date();
> Mon May 26 2014 10:55:34 GMT+0300 (Kaliningrad Standard Time)
> now.toUTCString();
> Mon, 26 May 2014 07:55:34 GMT
> new Date(now.toUTCString())
> Mon May 26 2014 10:55:34 GMT+0300 (Kaliningrad Standard Time)
When you create a new Javascript date object and assign it to a Mongo date field, it will automatically be usable with any timezone. If all you need is the current time, you can just use new Date(). But if you need to take a string and have it be interpreted in a specific timezone, or you are going to be presenting a time to a user in a specific timezone, I highly recommend checking out Moment Timezone.
This code will take a date object stored in a Mongo model (which could be 1AM on January 1, 2014 in Sydney, Australia) and present it formatted to a Los Angeles timezone (which would still be December 31, 2013).
collection.find({_id: 123}, function(err, model) {
userDate = moment(model.date).tz("America/Los_Angeles").format("MM/DD/YYYY");
});
And if you ever need to parse a string that doesn't specify a UTC offset as a specific timezone, you can use the constructor:
moment.tz("2013-11-18 11:55", "America/Toronto")