Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Datepicker Not showing correctly

Tags:

reactjs

I am creating an appliscation using React datepicker but it not displayed correctly even when i copy paste from their example

<DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      showTimeSelect
      timeFormat="HH:mm"
      timeIntervals={15}
      timeCaption="time"
      dateFormat="dd-MM-yyyy HH:mm"
    />

in my apps seems like this enter image description here

What's possibly wrong?

like image 248
Matius Nugroho Aryanto Avatar asked Dec 04 '25 12:12

Matius Nugroho Aryanto


1 Answers

Using functional component

import React, { useState } from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";

const App = () => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      showTimeSelect
      timeFormat="HH:mm"
      timeIntervals={15}
      timeCaption="time"
      dateFormat="MMMM d, yyyy h:mm aa"
    />
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Using class based component

import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";

class App extends React.Component {
  state = {
    startDate: new Date()
  };
  setStartDate = picked => {
    this.setState({ startDate: picked });
  };
  render() {
    //const [startDate, setStartDate] = useState(new Date());
    return (
      <DatePicker
        selected={this.state.startDate}
        onChange={date => this.setStartDate(date)}
        showTimeSelect
        timeFormat="HH:mm"
        timeIntervals={15}
        timeCaption="time"
        dateFormat="MMMM d, yyyy h:mm aa"
      />
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

you can see example for your reference.

Date picker example

like image 175
akhtarvahid Avatar answered Dec 07 '25 09:12

akhtarvahid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!