How can I change flatpickr default timezone - flatpickr

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.

Related

Setting and retrieving DateTime using Datetime-local HTML type

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"

Google's idToken exp always set to 1970

After receiving idToken from Google and verified, I noticed its exp: 1623186214 is always set to 1970 which will always expire itself. For example var when=new Date(1623186214); resulting Mon Jan 19 1970 10:53:06 GMT-0800 (Pacific Standard Time).
How to address it? Thank you in advance.
Okay, the exp is in seconds since midnight 01 January 1970 GMT.
So the correct way to check expiration is
var d=new Date('1970-01-01T00:00:00Z'); // 1970-01-01 GMT
d.setUTCSeconds(1623259934); // the value of 'exp', note use UTC not setSeconds().
Now it's Wed Jun 09 2021 10:32:14 GMT-0700 (Pacific Daylight Time), one hour after log in.
However, some better ways to validate it:
use Google's public endpoint
use Google's client library google-auth-library
Details on Authenticate with a backend server
JWT timestamp in seconds, but Javascript Date requiring milliseconds. But both JWT and Javascript Date constructor use the idea “since the epoch”. So just multiple exp by 1000.
new Date(exp * 1000)


Why and how mongoose save in mongodb with ISODate but when retrieve this variable is in local time? (node)

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.

Make node.js use system timezone

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}

How to store datetime on server side in nodejs?

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

Resources