Reputation: 977
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);
Upvotes: 2
Views: 1864
Reputation: 8102
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
Upvotes: 1