arqex / react-datetime

A lightweight but complete datetime picker react component.
2k stars 870 forks source link

Init value is not working #782

Open GoldStar115 opened 3 years ago

GoldStar115 commented 3 years ago

I'm Submitting a ...

[x] Bug report
[ ] Feature request
[ ] Support request

Steps to Reproduce

here is the code

<Datetime
    className="datetimepicker"
    dateFormat={false}
    timeFormat="HH:mm A"
    initialValue={moment(this.state.start_time)}
    initialViewDate={moment(this.state.start_time)}
    onChange={this.handleTimeChange}
    onClose={this.handleTimeChange}
/>

Expected Results

Even though i set init value with moment object, It is showing the current time

Actual Results

Minimal Reproduction of the Problem

Other Information (e.g. stacktraces, related issues, suggestions how to fix)

lightreign commented 3 years ago

also experiencing this

johnhunter commented 3 years ago

I cannot reproduce based on the repro example. If the initialViewDate is a valid moment date then it should work. Docs state that you can provide a string date, as it is parsed by moment internally, but you will need to ensure the string is in the format specified in your timeFormat prop.

let startValue;
startValue = moment().subtract(2, 'hours'); // this works
startValue = '09:30 pm'; // this also works
const handleTimeChange = v => console.log(String(v));
return (
 <Datetime
    dateFormat={false}
    timeFormat="HH:mm A"
    initialValue={startValue}
    initialViewDate={startValue}
    onChange={handleTimeChange}
    onClose={handleTimeChange}
 />
);

If you're still seeing the issue can you feedback the react-datetime version you're using, and the prop values used to reproduce the issue?

alexintelichain commented 1 year ago

@johnhunter Can U please help "react-datetime": "^3.2.0",

`

const defaultValue =   moment(forecastDate).add(1, 'month') // Sat Apr 01 2023 00:00:00 GMT+0300 as Moment Object 
<Datetime
      dateFormat="MMMM YY"
      initialViewMode={'years'}
      timeFormat={false}
      initialValue={defaultValue}
      // isValidDate={}
      onChange={onChangeDate}
      inputProps={{ placeholder }}
      renderMonth={renderMonth}
      renderYear={renderYear}
    />

But its actually showing = September 23 Only this case it's correct defaultValue = moment(forecastDate).add(1, 'month').subtract(5, 'month');

johnhunter commented 1 year ago

Hey @alexintelichain I'm not sure I can help 😞 I'm not a maintainer and no longer work on projects that use react-datetime. I'll see if I can figure out the issue when I get a chance, but can't promise anything.

johnhunter commented 1 year ago

Can U please help

@alexintelichain do you have a repo that recreates the issue? It would help enormously if you can share or create one.

If you do, make sure that it includes an npm or yarn lock file. As noted in https://github.com/arqex/react-datetime/issues/780 without a lock file in the project there is no way to ensure you have the same dependency versions as the last released version of react-datetime. That makes it hard to reproduce reported bugs.