tcomb-form-native icon indicating copy to clipboard operation
tcomb-form-native copied to clipboard

Format date value returns time

Open moore82 opened this issue 6 years ago • 4 comments

Version

  • tcomb-form-native v0.6.20
  • react-native v0.57.8

[email protected] │ └─┬ [email protected] │ └── [email protected]

I'm having trouble with formatting the date value that is returned.

FORM FIELD SETUP

const FormFilters = t.struct({
  dates: t.maybe(t.list(t.String)),
  fromDate: t.maybe(t.Date),
  toDate: t.maybe(t.Date),
  category: t.maybe(t.list(t.String)),
});

FORM OPTIONS

fromDate: {
      mode: 'date',
      label: 'FROM DATE',
      error: '',
      config: {
        format: date => {
          const newDate = moment(date).format('YYYY-MM-DD');
          return newDate;
        },
        dateFormat: date => {
          const newDate = moment(date).format('YYYY-MM-DD');
          return newDate;
        },
      }
    },

FORM SETUP

<Form 
    ref={form => this._filtersform = form}
    type={FormFilters}
    options={FormFiltersOptions}
    value={this.state.filtersForm}
    onChange={this._onChange}
/>

I would expect the output to be: 2019-01-02

The value that is returned however is: 2019-01-02T00:00:00.000Z

What am I doing wrong?

moore82 avatar Jan 22 '19 10:01 moore82

Up ! @moore82 Were you able to get the desired date format ? I'm struggling on this one too

drowolath avatar Feb 27 '19 12:02 drowolath

Hello again, I actually got this working.

First add moment to your project (yarn add moment or npm install moment --save)

Then, in your form options you can add the following:

fromDate: {
      mode: 'date',
      label: 'FROM DATE',
      error: '',
      config: {
        format: (date) => {
          return moment(date).format('YYYY-MM-DD');
        },
        dateFormat: (date) => {
          return moment(date).format('YYYY-MM-DD');
        },
      }
    },

That actually worked for me

drowolath avatar Feb 28 '19 12:02 drowolath

I have the same problem, @drowolath solution didn't work for me

ahmadyousefdev avatar Nov 19 '19 18:11 ahmadyousefdev

What solved it for me is that I reconverted the value with moment after the value were set, for example:

const value = this.refs.form.getValue();
from_time: moment(value.fromTime).format('HH:MM:SS'),

ahmadyousefdev avatar Nov 19 '19 18:11 ahmadyousefdev