Tcomb-form-native: How to set default value to date

Created on 25 Aug 2017  路  9Comments  路  Source: gcanti/tcomb-form-native

I stock my date to data base like this 'MM-DD-YYYY hh:mm:ss' but when I'm trying to select this date and set It into a date field form this error was appear 'Invalid date'

const dateField= t.struct({
    date1: t.maybe(t.Date),
    date2: t.maybe(t.Date)
});
const defaultValue = {
 date1: select_from_table_date1,
 date2: select_fromtable_date2
}

the select work without problem and I show It by console.log, the result like this 'MM-DD-YYYY hh:mm:ss'

Most helpful comment

Had the same problem and solved it by setting a default value to the date
Considering I have :
````
var CreateAccount = t.struct({
birthDate: t.Date,
});

var maxDate = new Date();
maxDate.setFullYear(maxDate.getFullYear()-18);

var createOptions = {
auto: 'placeholders',
fields: {
birthDate: {
config: {
format: (date) => moment(date).format('DD/MM/YYYY'),
},
maximumDate: maxDate,
mode: 'date',
blurOnSubmit: true
}
}
}
````

this.createValue = {'birthDate': maxDate};

And setting it as default value
<Form ref="formCreate" type={CreateAccount} options={createOptions} value={this.createValue} onChange={(value) => this.createValue = value}/>

Hope it helps

All 9 comments

Try https://momentjs.com/
Probably is the parse from server to javascript.

You should pass a date instance: new Date(/*options here*/).

Had the same problem and solved it by setting a default value to the date
Considering I have :
````
var CreateAccount = t.struct({
birthDate: t.Date,
});

var maxDate = new Date();
maxDate.setFullYear(maxDate.getFullYear()-18);

var createOptions = {
auto: 'placeholders',
fields: {
birthDate: {
config: {
format: (date) => moment(date).format('DD/MM/YYYY'),
},
maximumDate: maxDate,
mode: 'date',
blurOnSubmit: true
}
}
}
````

this.createValue = {'birthDate': maxDate};

And setting it as default value
<Form ref="formCreate" type={CreateAccount} options={createOptions} value={this.createValue} onChange={(value) => this.createValue = value}/>

Hope it helps

@ishigamii, why this is showing error "moment is not defined"?

@nhmasum cause you need to install moment via npm and then import it
import moment from 'moment';

@ishigamii Thanks for your input. Could you please tell me what's the date format you chose to set myDate variable ? I tried everything from ISOString(), to UTC, etc. Nothing works.
I am able to set default values for inputs but not the dates or time.
I have my dates coming from server in the DD/MM/YYYY format and the time in HH:mm format. I am using moment but not able to set the date when I set the value when the component loads.
Awaiting your response. Thanks in advance !

@kkrishnan90 sorry bad copy paste, myDate is maxDate which is a new Date().
I edited my code ;) Hope it helps

Yeah okie. Got it. Thanks @ishigamii

thanks @ishigamii it solve my problem, thank youuu arigatou

Hi, am trying to use datetime picker. however i need only a single placeholder together. however it shows two placeholders.. tap to select date and tap to select time
pls help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flyingace picture flyingace  路  5Comments

sibelius picture sibelius  路  5Comments

chauthai picture chauthai  路  4Comments

scarlac picture scarlac  路  4Comments

timdoes picture timdoes  路  4Comments