Integration with JSON

Generally, the data which is received from the server is serialized in a JSON format.

The date object in JSON is an ISO8601-formatted date string. For more details, refer to the documentation on the JSON.stringify method. On the other hand, the TimePicker works only with JavaScript Date instances.

To bind the TimePicker to dates which are serialized as strings, handle the parsing process:

  1. Convert the JSON date strings into valid JavaScript Date objects by using the IntlService or any other suitable Date parser.
  2. Define the value property of the component.
  3. Provide an onChange event handler to the TimePicker to get the selected Date value.

The following example demonstrates how to set the value of the TimePicker.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { TimePicker } from '@progress/kendo-react-dateinputs';
import { provideIntlService } from '@progress/kendo-react-intl';

class App extends React.Component {
    constructor(props) {
        super(props);

        const model = JSON.parse('{ "departureTime": "2017-06-30T12:22:32Z" }');
        model.departureTime = provideIntlService(this).parseDate(model.departureTime, 'yyyy-MM-dd HH:mm:ss');

        this.state = {
            model: model
        };
    }
    handleChange = (event) => {
        this.setState({ model: { departureTime: event.target.value } });
    }
    render() {
        return (
            <div>
                <div className="example-config">
                    <p>Model: {JSON.stringify(this.state.model)}</p>
                    <p>TimePicker value: {`${this.state.model.departureTime}`}</p>
                </div>
                <label>
                    Select Birthdate:
                    <TimePicker
                        value={this.state.model.departureTime}
                        onChange={this.handleChange}
                    />
                </label>
            </div>
        );
    }

}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

In this article