0

I rarely program JavaScript also React. I am using React. I mimic this solution https://stackoverflow.com/a/16978396/3728901 . My code

    const timeTemplate = (data, props) => {
        let formatted;
        return (
            <>
                <span className="p-column-title">{props.header}</span>
                {(new Date(data[props.field])).format("yyyy-mm-dd")}
                {/*var t = new Date( {data[props.field]} );*/}
                {/*var formatted = t.format("dd.mm.yyyy hh:MM:ss");*/}
                {/*{formatted}*/}
            </>
        );
    };

My full source code https://gist.github.com/donhuvy/30dcf82a7317d7ad6001e6d241eced08#file-timeperiod-js-L119

My error

enter image description here

How to fix it?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • 1
    `Date` has no method `format`. In that post, they are using [moment.js](https://momentjs.com/). It is kinda confusing in that post, but you can read it in the comments. – Chris Aug 06 '21 at 10:07
  • Indeed, the correct syntax would be `moment(new Date(data[props.field])).format(...)`, provided you have *installed* [Moment](https://momentjs.com/) and you have *imported* the dependency. Moment however is being [phased out](https://momentjs.com/docs/#/-project-status/). One nice alternative is [date-fns](https://date-fns.org/) (see [format](https://date-fns.org/v2.23.0/docs/format)). – Nikos Paraskevopoulos Aug 06 '21 at 11:53

0 Answers0