-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[YearPicker] 1 is extracted from year when timezone is +00:00 #6616
Comments
hi @Oktay28, I could not recreate this issue. check this demo https://codesandbox.io/s/dawn-sea-k1uprr?file=/demo.tsx |
@yaredtsy is your timezone +00:00 ? @alexfauquette I do not manage it, I just pass "2017" as value and because of my timezone it is displayed as 2016 I use this for localication: |
The problem might be in the string parsing. timezone management of |
Both cases work the same way. 2017 is not integer in both cases, because integer is for milliseconds. "2017" or new Date("2017") as value result to same displayed value "2016".
I think the issue comes from here. If I pass "2017-02-02" I get displayed value 2017. But when it is only the year "2017", the displayed value is 2016 |
Ok, so setting a full date and not only a year solves your problem? |
I only explained what I think is causing the problem. I do not want to format the value like this. The server sends me the truth. The truth is 2017. I do not want to make string or date manipulations to hack that behaviour. Does it look normal to you when "2017" is passed the result to be 2016? is this the expected behaviour from the datepickers? |
The expected behavior is to should the full year provided. The ability to pass a string is just sugar syntax. Behind the scene, all we do is parse the string you provided to Since the parsing is done behind the scene lot of people hope it would work magically. That's why in v6 we will remove the string support.
Since Form
|
Passing value as string or Date does not matter for me. The problem is that I do not want to convert it to "2017-02-01" or date.setMonth(1) // (Feb) before passing it as value. When new Date("2017") is provided as value to DateTimePicker the displayed value is "12/31/2016 11:00 pm" with timezone +00:00 I may do some tricks to add month to the "2017" as "2017-02-01" to make it think it is Feb so I can get the right year. But how I am supposed to store that value "2017-01-01T00:00:00Z" and use it on DateTimePicker What solution you recommend for passing value as "2017" or new Date("2017") and get the same displayed value as the value prop? |
For timezone issues with date-fns, better to ask directly on date-fns. Here you have some StackOverflow question related to your problem of parsing a string with timezone. |
I tend to agree—timezones and it's management is generally tricky and always has some edge-cases. Consider the examples below. They clearly illustrate that the date is not initialized to the value you would expect—unless time is also provided. new Date('2017');
// Sat Dec 31 2016 23:00:00 GMT-0100 (Azores Standard Time)
new Date('2017-01-01');
// Sat Dec 31 2016 23:00:00 GMT-0100 (Azores Standard Time)
new Date('2017-01-01T00:00');
// Sun Jan 01 2017 00:00:00 GMT-0100 (Azores Standard Time) @Oktay28 Could you please consider initializing the date with the expected format (as @alexfauquette has mentioned in the spec—JS Date expects a date in ISO format, and it's minimal form is |
I changed my timezone via extension for Chrome. Neither that format When I tested changing it via Chrome dev tools both solutions worked. I am not sure in real situation what people gonna get as displayed value. It seems like |
I can confirm that changing timezone using extension and timezone settings on Mac did behave differently. What do you think about opting to use UTC time explicitly by suffixing new Date('2017-01-01T00:00:00Z');
// Sun Jan 01 2017 00:00:00 GMT-0100 (Azores Standard Time) This consistently produces same result in both computer changed time zone as well as time zone changed by an extension. |
I have already tried that. new Date("2017-01-01T00:00:00Z").getFullYear() returns 2016 |
when you change the timezone, the time also changes. if your local time is |
Okay, I've done more testing and reading around the Date parsing docs. P.S. Do not use browser extension to test this change as we are not sure what it is actually doing—its also producing strange results for me. |
IMO the core issue is here that there are two different ways how dates can be understood:
The latter use-case is hard to implement with mui at the moment. To do this properly:
ExampleSandbox: https://codesandbox.io/s/relaxed-mirzakhani-6tsyym?file=/demo.tsx The upper is the date picker used as is, the lower is a modified version with the fixes detailed above. Then, I edit the date in both textfields (with the picker the timezone is not set, hence this issue does not occur). Now, the sandbox shows in UTC+1 wintertime (so UTC+2): Note that this sandbox only shows the weird behavior around storing the date. A similar problem occurs when reading the date; e.g. in GMT-6 the date picker would show the date before the expected date (as midnight UTC is in the previous day in GMT-6). Proposed fixA clean solution would be to explicitly separate the behaviors where a timestamp is needed, and where it is not. In the latter, the The documentation could however at least be extended in that way. There is already an issue for this: #7769 |
@famoser If I'm not mistaken, you are talking about the need to have |
@LukasTy Thank you for the link! Although it still requires wrapping the However, this would still be a timestamp, and therefore not fully capture the second use-case I've mentioned above (e.g. birthdate). A concrete example here would be a user choosing their birthday, resulting in the timestamp Instead, the |
JavaScript and most date manipulation libraries for it operate on the notion that Date is a timestamp, rather than having a distinction between
If your code assumes that you are operating with UTC dates in every step along the data chain—you shouldn't end up in the mentioned case. |
Thank you @LukasTy, those are some good points. I think in my ideal world I'd like to work on the
so not sure if Mui doing something different is such a good idea.
This is what we are going for now; but this behavior is subtle. Everyone has to "know" this is a UTC date and not a real timestamp, so use different formatting functions. So to summarize:
Thank you for your help. |
I also ran into this with |
Duplicates
Latest version
Current behavior 😯
When "2017" or new Date("2017") is passed as value to DatePicker with views ['year'], the displayed value in the input is 2016. This happens when the timezone is +00:00
timezone +03:00 : displayed value is 2017 when the given value is "2017". Displayed value is same as given value
timezone +00:00 : displayed value is 2016 while the given value is "2017"
Expected behavior 🤔
Displayed value to be exactly the same as the given value
Steps to reproduce 🕹
Pass value="2017" and views={['year']} to MobileDatePicker. Timezone should be +00:00
Adapter: AdapterDayjs or AdapterDateFns
Context 🔦
I receive integer from server to use it as value. I convert it to string, because integer as value is for milliseconds. The truth is "2017", but it is displayed as 2016
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: