【问题标题】:Subtract 2 times in react-native在 react-native 中减去 2 次
【发布时间】:2019-02-04 09:41:38
【问题描述】:

我想减去两次并检查 check_in time > 12Hr 然后导航到 checkin 屏幕。这是我的代码 sn-p

 class SplashView extends Component {

    componentDidMount() {
        const curr_time = moment().format('hh:mm:ss');
        const checkin_time = moment(this.props.datetime).format('hh:mm:ss');
        // const diff = moment.duration(checkin_time.diff(curr_time));
        console.log("TIME&&&&&&&&", curr_time, checkin_time);
        setTimeout(() => {
            if (this.props.employee_id === null) {
                this.props.navigation.navigate('Login');
            } else {
                this.props.navigation.navigate('Checkin');
            }

        }, 1000)
    }



    render() {
        return ();
    }
}

我正在使用 moment.js,this.props.datetime 的值为 '2019-02-04 14:52:01'。这是我的入住时间。

【问题讨论】:

    标签: react-native momentjs


    【解决方案1】:

    您可以像这样以小时为单位获得差异:

    const diff = moment.duration(checkin_time.diff(curr_time)).as('hours');
    

    或者你可以只使用 moment 的 diff 函数

    const diff = checkin_time.diff(curr_time, 'hours')
    

    然后比较if (diff > 12) { ... }

    【讨论】:

    • 我收到一个错误“未定义不是函数(正在评估 'checkin_time.diff(curr_time, 'hours')')。
    • 尝试在不格式化 'hh:mm:ss' 中的两个时间的情况下执行 checkin_time.diff(curr_time, 'hours')
    • 错误消失了,但我得到这样的输出 { _isAMomentObject: true, _isUTC: false, _pf: { empty: false, usedTokens: [], usedInput: [], overflow: -2, charsLeftOver: 0, nullInput: false,
    • 这是一个时刻对象,你到底在记录什么?这? console.log(checkin_time.diff(curr_time, 'hours')) 这应该打印一个数字(以小时为单位)
    • 我把它当作 0
    猜你喜欢
    • 2019-09-07
    • 2018-01-30
    • 2011-07-12
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多