简体   繁体   中英

What is correct date format in material-ui?

I have been trying to get currentDate in material ui which i fairly managed to get and onClick of date ,we get current Date but the default display shows dd/mm/yyyy instead of showing the exact date . I want the date to be displayed instead of dd/mm/yyyy . Here is the modified code in material ui and here is the link to codesandbox sandbox

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 200,
  },
});
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();

if (dd < 10) {
  dd = '0' + dd
}

if (mm < 10) {
  mm = '0' + mm
}

today = mm + '/' + dd + '/' + yyyy;
//document.write(today);
function DatePickers(props) {
  const { classes } = props;

  return (
    <form className={classes.container} noValidate>
      <TextField
        id="date"

        type="date"
        defaultValue={today}
        className={classes.textField}
        InputLabelProps={{
          shrink: true,
        }}
      />
    </form>
  );
}

DatePickers.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DatePickers);

You have wrong format of date value.

Try this:

today = yyyy + '-' + mm + '-' + dd;

here is working codesandbox: https://codesandbox.io/s/8z6v3qj782

PS if you see any mistake then at least give explanation rather just giving downvotes. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM