Skip to content
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

[pickerts] not correctly setting the date time using dayjs with timezone #11955

Closed
Olzindel opened this issue Feb 6, 2024 · 8 comments
Closed
Labels
component: pickers This is the name of the generic UI component, not the React module! external dependency Blocked by external dependency, we can’t do anything about it

Comments

@Olzindel
Copy link

Olzindel commented Feb 6, 2024

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/miu-datepicker-bug-date-5npl6g

Steps:

  1. Set up Localization provider with dayjs adapter with timezone plugin.
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.extend(utc).extend(timezone);

// ...
<LocalizationProvider dateAdapter={AdapterDayjs}>
      // ...
</LocalizationProvider>
  1. Simply use the textfield input of the DesktopDatePicker

Current behavior

The date returned by the DesktopDatePicker has a time, depending of the timezone.

Expected behavior

The time returned by the DesktopDatePicker should be similar as the one returned when there is no timezone involved (i.e. "00:00:00"), but relative to the timezone.
For example, if the inputed date is 01/01/2020 (in MM/DD/YYYY format) in a GMT+0100 environment, it should return the date 2021-12-31T23:00:00.000Z

Context

No response

Your environment

npx @mui/envinfo
  System:
    OS: macOS 14.2.1
  Binaries:
    Node: 20.8.1 - ~/.nvm/versions/node/v20.8.1/bin/node
    npm: 8.19.3 - ~/repos/main/node_modules/.bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 119.0.6045.199
    Edge: Not Found
    Safari: 17.2.1

Search keywords: dayjs timezone datepicker

@Olzindel Olzindel added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 6, 2024
@coratgerl
Copy link

I encountered the same issue 😔

@Olzindel
Copy link
Author

Olzindel commented Feb 6, 2024

Note that I think I fixed this issue on my codebase by doing something like

export const CustomDesktopDatePicker = () => {
	const [lastDate, setLastDate] = useState(null)

	return (
		<DesktopDatePicker
			onChange={(date) => {
				const nativeDate = date?.toDate()

				if (!nativeDate) {
					return
				}

				if (
					lastDate &&
					nativeDate.getTimezoneOffset() !==
						lastDate.getTimezoneOffset()
				) {
					nativeDate.setHours(0, 0, 0, 0)
				}

				if (dayjs(nativeDate).isValid()) setLastDate(nativeDate)
			}}
		/>
	)
}

@michelengelen
Copy link
Member

michelengelen commented Feb 6, 2024

The problem is that the introduction of UTC and timezones is faulty in Dayjs.
I would advice you to use moment instead:

import { useState } from 'react';
import { Moment } from 'moment';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { LocalizationProvider } from '@mui/x-date-pickers';

const CompTest = () => {
  const [value, setValue] = useState<Moment | null>(null);
  return (
    <LocalizationProvider dateAdapter={AdapterMoment} adapterLocale={'de'}>
      <DesktopDatePicker
        value={value}
        onChange={(value) => {
          setValue(value);
        }}
      />
      <p>Date: {value?.toString()}</p>
    </LocalizationProvider>
  );
};

export default CompTest;

There are open issues on the dayjs packages (and even open PRs - one from me), but the maintainer seems to be inactive. Example PR


EDIT

Example with Luxon below: #11955 (comment)

@michelengelen michelengelen added external dependency Blocked by external dependency, we can’t do anything about it component: pickers This is the name of the generic UI component, not the React module! status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 6, 2024
@michelengelen michelengelen changed the title DesktopDatePicker not correctly setting the date time using dayjs with timezone [pickerts] not correctly setting the date time using dayjs with timezone Feb 6, 2024
@LukasTy
Copy link
Member

LukasTy commented Feb 6, 2024

I would advice you to use moment instead:

Would using luxon also work? 🤔
moment has been in maintenance mode for quite some time, IMHO, we should not be recommending users jump into this library at this point. 🙈

@michelengelen
Copy link
Member

My apologies ... I was not aware that it is in maintenance mode. I just picked it because I am the most familiar with this package! :D
Of course you can also do this with luxon instead. Here is an example implementation for it:

import { useState } from 'react';
import { DateTime } from 'luxon';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { LocalizationProvider } from '@mui/x-date-pickers';

const CompTest = () => {
  const [value, setValue] = useState<DateTime | null>(DateTime.utc(2024, 1, 1, 0, 0, 0, 0));
  return (
    <LocalizationProvider dateAdapter={AdapterLuxon} adapterLocale={'de'}>
      <DesktopDatePicker
        value={value}
        onChange={(value) => {
          setValue(value);
        }}
      />
      <p>Date: {value?.toString()}</p>
    </LocalizationProvider>
  );
};

export default CompTest;

@Olzindel
Copy link
Author

Olzindel commented Feb 6, 2024

Thanks for your replies!

Unfortunately, the project I am working on recently made the switch from moment to dayjs, and I can't make the switch to Luxon, because of the differences in API between Luxon and Dayjs.
I think I'm going to wait a release of a fix for this problem by DayJS mainteners and stay with the fix I sent in my previous message for the meantime.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Feb 6, 2024
@michelengelen
Copy link
Member

Thanks for your replies!

Unfortunately, the project I am working on recently made the switch from moment to dayjs, and I can't make the switch to Luxon, because of the differences in API between Luxon and Dayjs. I think I'm going to wait a release of a fix for this problem by DayJS mainteners and stay with the fix I sent in my previous message for the meantime.

Good to hear that your solution for this is working for you. 👍🏼
And sorry that we could not help you in a better way! 🙇🏼

@michelengelen
Copy link
Member

I will close this issue for now, but feel free to reopen if you have any further questions regarding this topic!

@michelengelen michelengelen removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: pickers This is the name of the generic UI component, not the React module! external dependency Blocked by external dependency, we can’t do anything about it
Projects
None yet
Development

No branches or pull requests

4 participants