Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: utils.endOfDay is not a function

I am new in react, using material-ui date picker in my project , from the documentation here . In this i am using moment.js

the following code in datetimepicker

    import React,{ useState }  from 'react';
    import MomentUtils from 'moment';
    import {
        DatePicker,
        TimePicker,
        DateTimePicker,
        MuiPickersUtilsProvider,
     } from "@material-ui/pickers";

    function DateTimePickers() {
         const [selectedDate, handleDateChange] = useState(new 
            Date());

          return (
             <MuiPickersUtilsProvider utils={MomentUtils}>
             <DatePicker value={selectedDate} onChange= 
              {handleDateChange} />
              <TimePicker value={selectedDate} onChange= 
               {handleDateChange} />
                <DateTimePicker value={selectedDate} onChange= 
              {handleDateChange} />
            </MuiPickersUtilsProvider>
          );
        }
    export default DateTimePickers;

but showing error

TypeError: utils.endOfDay is not a function getComparisonMaxDate node_modules/@material-ui/pickers/esm/_helpers/text-field-helper.js:23

  20 |     return date;
  21 |   }
  22 | 
> 23 |   return utils.endOfDay(date);
  24 | };
  25 | 
  26 | var getComparisonMinDate = function 
       getComparisonMinDate(utils, strictCompareDates, date) {

Thanks in advance . Help me out of this problem

like image 349
ajeesh s Avatar asked Aug 23 '19 07:08

ajeesh s


People also ask

What does TypeError X is not a function mean?

TypeError: "x" is not a function The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function.

What does the JavaScript error “is not a function” mean?

The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function. Message TypeError: Object doesn't support property or method {x} (Edge) TypeError: "x" is not a function

What does the JavaScript exception'is not a function'mean?

The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function. What went wrong? It attempted to call a value from a function, but the value is not actually a function. Some code expects you to provide a function, but that didn't happen.

Why can't I call diff() from a dayjs object?

Make sure that Day.js supports the require () function. If it is import -based, you need to maybe change the import to that. So, just try doing the following. Your were trying to call diff () on a string (the output of dayjs ().format ()) rather than a Dayjs object


2 Answers

The import is wrong. It should be:

import MomentUtils from '@date-io/moment';

You can refer to working CodeSandbox demo:https://codesandbox.io/s/material-demo-h3ke7?fontsize=14

Clarification:

  • date-io provides abstraction over common javascript date management libraries. date-io/moment is just one of them. data-io GitHub
  • You must install both moment and data-io/moment
  • Material-ui/pickers fully relies on data-io. You must install it if you want to use material-ui pickers. You can further read in material-ui-pickers GitHub
like image 90
Ido Avatar answered Oct 13 '22 19:10

Ido


I faced the same issue in date picker documentation https://material-ui-pickers.dev/getting-started/installation

install thie version npm i @date-io/[email protected] moment

like image 44
Mr.Throg Avatar answered Oct 13 '22 21:10

Mr.Throg