7

I have a simple date input component, i just stuck when change the value of it. Because it always print value "1970-01-01".

Somebody knows how to do it. would appreciate thanks.

Here is my codes:

import React, { useState } from 'react';
import moment from 'moment';

const DateInput = ({ className = style.defaultSize, onChange }) => {
  const [value, setValue] = useState(moment().format('YYYY-MM-DD'));

  const onChangeDate = (date) => {
    const newDate = moment(date.timeStamp).format('YYYY-MM-DD');
    setValue(newDate);
    console.log(newDate); //always log "1970-01-01"
  };

  return (
    <input
        type="date"
        value={value}
        onChange={onChangeDate}
     />
  );
};

export default DateInput;
2
  • The question is missing details about the problem. Please elaborate on your problem so that i can underatand Commented Jun 7, 2021 at 5:04
  • date.timeStamp should be date.target.value in the onChangeDate function. const newDate = moment(date.target.value).format("YYYY-MM-DD");. 👉 SandBox: Commented Jun 7, 2021 at 5:11

3 Answers 3

3

That because we will get the input value inside the event.target.value

  const onChangeDate = e => {
    const newDate = moment(new Date(e.target.value)).format('YYYY-MM-DD');
    setValue(newDate);
    console.log(newDate); //value picked from date picker
  };
Sign up to request clarification or add additional context in comments.

Comments

2

I have extended your code and showed it away, you can try it or visit live code to Codesandbox!

import React, { useState } from "react";
import moment from "moment";
import Moment from "react-moment";


const DateInput = ({ className = style.defaultSize, onChange }) => {
  const [value, setValue] = useState(moment().format('YYYY-MM-DD'));

  const onChangeDate = (date) => {
    const newDate = setValue(moment(new Date(date.target.value)).format("YYYY-MM-DD"));
    setValue(newDate);
    console.log(newDate); //always log "1970-01-01"
  };

  return (
    <input
        type="date"
        value={value}
        onChange={(e)=>onChangeDate(e)}
     />
     
     <strong>MOMENT TIME:</strong>
      <br />
      <Moment format="DD-MM-YYYY">{value}</Moment>
  );
};

export default DateInput;

Thanks

3 Comments

Question slightly off topic, why is it that your example in Codesandbox doesn't have React's control input caret jumping to the end issue? I also made an example with a plain text input and it doesn't have that issue in that same sandbox. In my project I have an implementation very similar to yours but because of controlled input caret jumping, editing the date by text breaks.
@NikolasPafitis, I really don't understand your problem, hopefully, it's come from another issue in your code.
I'm referring to this issue stackoverflow.com/questions/46000544/… The sandbox example you provided doesn't have it although it should afaik.
0

You need to access the field value via target property of the event object of the input. So your code would change as below:

import React, { useState } from 'react';
import moment from 'moment';

const DateInput = ({ className = style.defaultSize, onChange }) => {
  const [value, setValue] = useState(moment().format('YYYY-MM-DD'));

  const onChangeDate = ({ target }) => {
    const newDate = moment(target.value.timeStamp).format('YYYY-MM-DD');
    setValue(newDate);
    console.log(newDate); //always log "1970-01-01"
  };

  return (
    <input
        type="date"
        value={value}
        onChange={onChangeDate}
     />
  );
};

export default DateInput;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.