Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date from Date Picker and show in the table? Material-UI. ReactJS

I use Date Picker component from Material-UI for React JS. I want to show the selected date on the table. A date is an object and I have an error when trying to show in a table row. How to do this?

import React, { Component } from 'react';
import DatePicker from 'material-ui/DatePicker';
import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';

export default class AddTaskDialog extends Component {
  constructor(props) {
    super(props);
    this.state = { controlledDate: {} };
    this.handleChangeDate = this.handleChangeDate.bind(this);
  }

  handleChangeDate = (event, date) => {
    this.setState({
      controlledDate: date,
    });
  };

  render() {
    return (
      <div>
        <DatePicker hintText="Date" value={this.state.controlledDate} onChange={this.handleChangeDate}/>
        <Table>
          <TableHeader>
            <TableRow>
              <TableHeaderColumn>Date</TableHeaderColumn>
            </TableRow>
          </TableHeader>
          <TableBody>
            <TableRow>
              <TableRowColumn>{this.state.controlledDate}</TableRowColumn>
            </TableRow>
          </TableBody>
        </Table>
      </div>
    );
  }
}
like image 384
Italik Avatar asked Sep 17 '25 04:09

Italik


1 Answers

The date that is sent to the handleChangeDate handler is of type object. You need to convert it to a date string in order to render inside the TableRowColumn.

export default class AddTaskDialog extends Component {
 constructor(props) {
 super(props);
 this.state = { controlledDate: new Date() };
 this.handleChangeDate = this.handleChangeDate.bind(this);
}

handleChangeDate = (event, date) => {
  this.setState({
    controlledDate: date
  });
};

// INSIDE RENDER
<TableRowColumn>{this.state.controlledDate.toDateString()}</TableRowColumn>

const date = new Date();

console.log(typeof date);
   
console.log(date.toDateString());
like image 182
Nandu Kalidindi Avatar answered Sep 19 '25 18:09

Nandu Kalidindi