question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not format textField of type date in the form 'DD/MM/YYYY'

See original GitHub issue

Can not format textField of type date in the form ‘DD/MM/YYYY’. The options for type are limited. Why not have a format field

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

TextField should have a format prop that will help format date in any format. eg.

<TextField
                onChange={onDateChange}
                id="date"
                label="Date Received"
                type="date"
                format={'DD/MM/YYYY'}
                value={dateRecieved}
                className={classes.textField}
                margin="normal"
                InputLabelProps={{
                  shrink: true
                }}
/>

Current Behavior

Currently the type options doesn’t allow to format in DD/MM/YYYY form.

Your Environment

Tech Version
Material-UI 1.0.0-beta.32
React 3.7.2
browser chrome
etc

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
oliviertassinaricommented, May 6, 2019

@yuvraj88 You have two options:

  1. Use @material-ui/pickers
  2. Use a custom inputComponent prop with the native implementation. It would probably only work well on mobile.

https://codesandbox.io/s/zq89ym8v74

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const useStyles = makeStyles(theme => ({
  root: {
    position: "relative"
  },
  display: {
    position: "absolute",
    top: 2,
    left: 0,
    bottom: 2,
    background: "white",
    pointerEvents: "none",
    right: 50,
    display: "flex",
    alignItems: "center"
  },
  input: {}
}));

function InputComponent({ defaultValue, inputRef, ...props }: any) {
  const classes = useStyles();
  const [value, setValue] = React.useState(() => props.value || defaultValue);

  const handleChange = event => {
    setValue(event.target.value);
    if (props.onChange) {
      props.onChange(event);
    }
  };

  return (
    <div className={classes.root}>
      <div className={classes.display}>{value}</div>
      <input
        className={classes.input}
        ref={inputRef}
        {...props}
        onChange={handleChange}
        value={value}
      />
    </div>
  );
}

function DatePickers() {
  return (
    <TextField
      id="date"
      label="Birthday"
      type="date"
      InputProps={{
        inputComponent: InputComponent
      }}
      defaultValue="2017-05-24"
      InputLabelProps={{
        shrink: true
      }}
    />
  );
}

export default DatePickers;

2reactions
MaheshKalyankarcommented, Feb 5, 2019

Its strange… it takes the local machine’s date time format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not format textField of type date in the form 'DD/MM/YYYY'
TextField should have a format prop that will help format date in any format. eg. <TextField onChange={onDateChange} id="date" label="Date ...
Read more >
Is there any way to change input type="date" format?
Users can type a date value into the text field of an input[type=date] with the date format shown in the box as gray...
Read more >
<input type="date"> - HTML: HyperText Markup Language
This code finds the first <input> element whose type is date , and sets its value to 2017-06-01 (June 1st, 2017).
Read more >
How to set input type date in dd-mm-yyyy format using HTML
To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used...
Read more >
Error transforming dd.mm.yyyy text field to date
I made a typo in the description, the date is DD.MM.YYYY format as stated in the subject. In any case, I am surprised...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found