【问题标题】:How to change the US default dateformat input fields in React-bootstrap-daterangepicker如何更改 React-bootstrap-daterangepicker 中的美国默认日期格式输入字段
【发布时间】:2016-12-09 14:49:38
【问题描述】:

我是 React 和 Javascript 的新手,遇到以下问题:我需要在我的 React-bootstrap-daterangepicker 中更改默认的美国日期格式。我找到了一些关于从 momentjs 站点更改语言环境对象的信息,但不确定如何实现它...我还尝试添加 dateFormat="DD/MM/YYYY" (和 format="DD/MM/YYYY" ) 属性到我的 DatePicker 但没有成功。 我的反应版本是 15.4.0 提前致谢!

import React from 'react';
import { ControlLabel, FormGroup, HelpBlock } from 'react-bootstrap';
import DatePicker from "react-bootstrap-daterangepicker";
import moment from 'moment';

class DateRangePicker extends React.Component {

    componentWillMount() {
        const value = new Date().toISOString().replace("T", " ").replace("Z", "");
        const date = value;

        this.setState({
            value: date,
            startDate: moment(),
            endDate: moment()

        });

    }

    handleChange(e, datepicker) {
        this.setState({

            startDate: datepicker.startDate,
            endDate: datepicker.endDate,
            value: datepicker.startDate + " to " + datepicker.endDate
        });
        const label = datepicker.startDate + " to " + datepicker.endDate;

        const {startDate, endDate} = datepicker;
        this.props.onSelect(startDate, endDate)
    }

    render() {
        let start = moment(this.state.startDate).format("DD/MM/YYYY");
        let end = moment(this.state.endDate).format("DD/MM/YYYY");
        let dateRange = start + ' to ' + end;
        return (
            <fieldset className="form-group form-group--small pull-left">
                <legend className="hidden">Choose a date range</legend>
                <FormGroup>
                    <ControlLabel className="hidden" htmlFor="dateRange">
                        Date range:</ControlLabel>
                    <DatePicker
                        readOnly="false"
                        startDate={this.start}
                        endDate={this.end}
                        onApply={this
                            .handleChange
                            .bind(this)}
                        onChange={this
                            .handleChange
                            .bind(this)}>
                        <div className="input-group input-group-small">
                            <div className="input-group-addon">
                                <i className="fa fa-calendar"></i>
                            </div>
                            <input type="text" id="dateRange" className="form-control" value={dateRange} />

                        </div>
                    </DatePicker>
                    <HelpBlock className="hidden">Help</HelpBlock>
                </FormGroup>

            </fieldset>

        );
    }
}

export default DateRangePicker;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

【问题讨论】:

    标签: javascript reactjs react-jsx bootstrap-datepicker


    【解决方案1】:

    根据documentation,它指的是original project,它又指向probably official site,看来你可以传递给setState,连同startDate等,一个@987654327 @子对象如locale: {format: 'DD-MM-YYYY'},如one of the examples所示。

    希望这会有所帮助!

    【讨论】:

    • 谢谢!我阅读了文档,但在语法上遇到了困难。不过,我现在已经设法解决了:)将发布它。
    【解决方案2】:

    我设法通过在我的组件中的 componentWillMount() 方法中初始化 locale const 来解决我的问题,即

        componentWillMount() {
        const value = new Date().toISOString();
        const date = value;
        const locale = locale;
    
        this.setState({
            value: date,
            locale: {
                'format': 'DD/MM/YYYY'
            },
            startDate: moment(),
            endDate: moment()
    
        });
       }
    

    然后为 DatePicker 标签设置一个语言环境属性:

     <DatePicker  locale={this.state.locale}
    

    ...

    【讨论】:

    • 太棒了!谢谢!我一直在到处寻找这个,我在一个没有投票的答案中遇到了它。
    • 谢谢你,@欧文!我很高兴它有帮助:) 如果你还没有这样做,你可以投票给它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2020-09-07
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多