【问题标题】:Manipulating dates Momentjs in ReactJS在 ReactJS 中操作日期 Momentjs
【发布时间】:2016-06-15 16:31:47
【问题描述】:

我有一个基本的 ES6 react 应用程序,我正在尝试使用 momentJS 来操作一些日期。出于某种原因,我不断收到month.add is not a function

目前我的代码是这样的:

export default class CalendarApp extends React.Component {

constructor() {
    super();

    this.state = {
        currentDate: Moment().format(),
        month: Moment().format('MMMM'),
        year: Moment().format('YYYY')
    }

    // Bind Methods to this
    this.previousMonth = this.previousMonth.bind(this);
    this.nextMonth = this.nextMonth.bind(this);
}

previousMonth() {
    let month = this.state.month;
    month.add(-1, 'months');
    this.setState({
        month: month
    })
}

nextMonth() {
    let month = this.state.month;
    month.add(1, 'months');
    this.setState({
        month: month
    })
}

render() {
    return (
        <div className="calendar">
            <div className="calendar-container" style={ hideCalendar }>
                <caption>
                    <button className="previous-month" onClick={ this.previousMonth }>Prev</button>
                    <button className="next-month" onClick={ this.nextMonth }>Next</button> 
                    <div className="calendar-month">
                        <span>{ this.state.month } { this.state.year }</span>
                    </div>
                </caption>
            </div>
        </div>
    )
}

}

我尝试过使用Moment().month() 等设置初始状态的各种版本,但似乎没有任何效果。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript reactjs state momentjs


    【解决方案1】:

    当您执行.format() 时,您将其转换为字符串,它不再是 momentJS 对象。

    moment().add(1, 'months') // A moment object
    
    moment().add(1, 'months').subtract(6, 'days') // Can still manipulate
    
    moment().add(1, 'months').subtract(6, 'days').format() // A final string, can't call moment funcs on it
    

    如果它们都在同一时间使用,则无需创建多个对象 -

    const momentNow = Moment();
    
    this.state = {
      currentDate: momentNow.format(),
      month: momentNow.format('MMMM'),
      year: momentNow.format('YYYY')
    }
    

    【讨论】:

    • 感谢您的回复。如果我只在状态下创建 1 个对象,例如 currentDate,那么以后是否可以使用this.state.currentDate.format("MMMM")?
    • 是的,您可以,尽管当前时间可能是在创建对象时而不是在您对其执行 .format 时进行的 - 可能不是问题,但值得牢记
    【解决方案2】:

    您的 state.month 是一个字符串。这就是问题的根源。

    试试这个

    constructor() {
        super();
    
        this.state = {
            currentDate: Moment(),
            month: Moment().format('MMMM'),
            year: Moment().format('YYYY')
        }
    
        // Bind Methods to this
        this.previousMonth = this.previousMonth.bind(this);
        this.nextMonth = this.nextMonth.bind(this);
    }
    
    previousMonth() {
        let date = this.state.currentDate;
        date.add(-1, 'months');
        this.setState({
            currentDate: date,
            month: date.format('MMMM'),
            year: date.format('YYYY')
        });
    }
    
    nextMonth() {
        let date = this.state.currentDate;
        date.add(1, 'months');
        this.setState({
            currentDate: date,
            month: date.format('MMMM'),
            year: date.format('YYYY')
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      相关资源
      最近更新 更多