How to show value, when using date type in TextField?

lo7

I am using TextField from material-ui, and I want to show current date in TextField, and also allow user to choose another date. If it is possible?

The value={date} do not appear in the TextField when using type="date". I have tried to find some help on the internet, but can't find anything. Code is below:

  • Any help is appreciated! And thanks in advance.

import React, { useState } from 'react';
import { TextField } from '@material-ui/core';
import { useForm } from 'react-hook-form';

export const AddDate: React.FC = () => {
    const [date, setDate] = useState(
        new Date().getDate() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getFullYear(),
    );

    // handles when user changes input in date inputfield
    const handleChangeDate = (e: React.ChangeEvent<HTMLInputElement>): void => {
       setDate(e.target.value);
    };

    return(
        {/*Text field - date*/}
        <TextField
            name="date"
            id="date"
            label="Date"
            type="date"
            InputLabelProps={{ shrink: true }}
            inputRef={register}
            value={date}
            onChange={handleChangeDate}
            fullWidth
            required
          />
    );
};
Dushan Randika

This works for me.

import React, { useState, useEffect } from "react";
import { TextField } from "@material-ui/core";
import "./styles.css";
import moment from 'moment';

const App =()=> {
  const [date, setDate] = useState(
    moment(new Date()).format("YYYY-MM-DD")
 );

 // handles when user changes input in date inputfield
 const handleChangeDate = e => {
    setDate(e.target.value);
 };

  console.log(date)

 return (
  <>
  <TextField
    name="date"
    id="date"
    label="Date"
    type="date"
    InputLabelProps={{ shrink: true }}
    value={date}
    onChange={handleChangeDate}
    fullWidth
    required
  />
</>
);
}

 export default App;

I just changed your code a little bit. Only change is comming from the useState. You just have to use moment js to convert the date into material-ui date type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to get the textfield value when i choose a date in DatePicker

how to retrieve date and show in input (type="Date") using ajax

How to show calendar popup when input[type="date"] is on focus

How to show Button Value in TextField in Flutter?

how to show datetimepicker and set the chosen value in textfield

How to show a blinking cursor when textfield is enable?

How to show the numpad automatically when the textfield is loaded?

How to show TextField when button is pressed?

How to extract the date value from input type date using jquery?

How to set value of a Date Textfield in Material UI

Textfield type date in React

how to set a default date for a Material-ui TextField Type=date

How to click on a textbox with date format and type in value using selenium with VBA?

How to get the time value alone when using input type=time, angular scope gets updated with date,time & timezone?

I am trying get date as a String type from a DateChooserPanel and show the date on a textfield

How to show value when user editing in form using angular

Change the dropdown value when particulare date is selected in jquery using input type date

How to update value for TextField using StreamBuilder?

How to convert the String date value in to date type value in power bi using DAX formula?

How to make the TextField widget editable when using controller as TextEditingController(text:"Enter value")?

How to show the selected value in input textfield in material ui autocomplete?

How to Show/Hide some elements based on value of a textfield?

How add quotes in a JSON string, using Java, when the value is a date

How to select the nearest date value in SQL using CASE WHEN expressions?

how to show xib view when tap on textfield right button

How can I access the fields of a data type when using `show` instead of the whole show string?

Get html date input type value and a value from dropdown to show in other div without loading page using javascript

Set dateValue on TextField type date

How to save value of TextField when a value is inputted in Flutter