Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI date picker Date format React

I can't see to format the Material UI Date picker in a React app??

When a user selects a date, I want it to format to be MM/DD/YYYY. I looked up a couple answers but it's not clear where the function to change the format is supposed to go??? For some reason, there is no clear function/location of where it goes online>>

DatePicker component

import React from 'react';
import DatePicker from 'material-ui/DatePicker';

/**
 * `DatePicker` can be implemented as a controlled input,
 * where `value` is handled by state in the parent component.
 */
export default class DateSelect extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      controlledDate: null,
      openSnackbar: false,
      snackbarText: {
        dateconf: 'Date has been entered!'
      }
    };
  }
  formatDate(date){


  return (date.getMonth() + 1) + "/" + date.getFullYear() + "/" +  date.getDate();
}
  handleChange = (event, date) => {
    console.log('we are in a handle change in date select', event, date)
    this.setState({
      controlledDate: date,
    });
    // this.setState({
        //  openSnackbar: true
        // })
  };

  render() {
    console.log('state and props in date select', this.state, this.props)

    return (
      <DatePicker formatDate={this.formatDate}
        hintText="Date of purchase"
        hintStyle={{color:'whitesmoke'}}
        inputStyle={{ color:'whitesmoke'}}
        value={this.state.controlledDate}
        onChange={this.props.onChange}
      />


    );
  }
}

     ///THE PARENT COMPONENT



   handleDatePicker = (name, date) => {
console.log('handling date change', name, date)
this.setState(prevState => ({
    currentRow: {
        ...prevState.currentRow,
        purchase_date: date
    },
    purchase_date: date,
    actionType: 'date'
}))
this.setState({
    openSnackbar: true
})

   }



                    <DateSelect 
                    hintText="Purchase date"
                    value = {this.state.purchase_date}
                    onChange={this.handleDatePicker}



                    />
like image 383
Just_some_Noob Avatar asked May 14 '18 01:05

Just_some_Noob


2 Answers

You need to use formatDate function to format the date and time in date picker

formatDate --> This function is called to format the date displayed in the input field, and should return a string. By default if no locale and DateTimeFormat is provided date objects are formatted to ISO 8601 YYYY-MM-DD.

<DatePicker
        hintText="Date of purchase"
        hintStyle={{color:'whitesmoke'}}
        inputStyle={{ color:'whitesmoke'}}
        value={this.state.controlledDate}
        onChange={this.props.onChange}
        formatDate={(date) => moment(new Date()).format('MM-DD-YYYY')}
      />

if you do not pass any format on the date it seems that the min and max date are not working.

For more details material-ui/DatePicker

like image 170
Ashh Avatar answered Sep 30 '22 02:09

Ashh


Straight from the documentation of DatePicker. Try this

<DatePicker 
        inputFormat="MM/dd/yyyy"
        hintText="Date of purchase"
        hintStyle={{color:'whitesmoke'}}
        inputStyle={{ color:'whitesmoke'}}
        value={this.state.controlledDate}
        onChange={this.props.onChange}
      />

inputFormat should usually do the trick. You can change it around the way you want and it can be used for dateTimePicker as well.

For example,

inputFormat="yyyy/MM/dd"

or any other way

like image 25
Crystal Killer Avatar answered Sep 30 '22 01:09

Crystal Killer