tcomb-form-native
tcomb-form-native copied to clipboard
Format date value returns time
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?
Up ! @moore82 Were you able to get the desired date format ? I'm struggling on this one too
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
I have the same problem, @drowolath solution didn't work for me
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'),