简体   繁体   中英

How to convert moment.js to date-fns

I am trying to use the code from this codepen in my react/date-fns app.

import {useEffect, useRef, useState} from 'react'
import {add, sub, format, parse} from 'date-fns'

const timeRegExp = /([01][0-9]|2[0-3])[0-5][0-9]/

const Time = () => {
    const refs = useRef([])
    const [values, setValues] = useState([])

    useEffect(() => {

        refs.current.forEach((input, i) => {
            input.addEventListener('keydown', (e: any) => {
                if (e.keyCode === 37) {// left arrow
                    if (i !== 0) refs.current[i - 1].focus()

                } else if (e.keyCode === 39) {// right arrow
                    if (i !== 3) refs.current[i + 1].focus()

                } else if (/48|49|50|51|52|53|54|55|56|57/.test(e.keyCode)) {// 0 ~ 9
                    const time = [0, 1, 2, 3].map(i => {
                        return i === i ? String.fromCharCode(e.keyCode) : refs.current[i].value
                    }).join('')
                    if (timeRegExp.test(time)) {
                        refs.current[i].value = String.fromCharCode(e.keyCode)
                        if (i !== 3) refs.current[i + 1].focus()
                    }
                } else if (e.keyCode === 8) {// delete / backspace
                    refs.current[i].value = 0
                    if (i !== 0) refs.current[i - 1].focus()

                } else if (e.keyCode === 38) {// up arrow
                    // if (i === 0 && refs.current[0].value === '2') {
                    //
                    // } else {
                    //     let time = [0, 1, 2, 3].map(i => refs.current[i].value).join('')
                    //     time = moment(time, 'HHmm').add(i % 2 ? 1 : 10, Math.floor(i / 2) ? 'm' : 'h').format('HHmm').split('');
                    //     [0, 1, 2, 3].forEach(i => refs.current[i].value = time[i])
                    // }
                } else if (e.keyCode === 40) {// down arrow
                    // if (i === 0 && refs.current[0].value === '0') {
                    // } else {
                    //     let time = [0, 1, 2, 3].map(i => refs.current[i].value).join('')
                    //     time = moment(time, 'HHmm').subtract(i % 2 ? 1 : 10, Math.floor(i / 2) ? 'm' : 'h').format('HHmm').split('');
                    //     [0, 1, 2, 3].forEach(i => refs.current[i].value = time[i])
                    // }
                }

                e.preventDefault()
            })
        })
        // input.addEventListener('keydown', e => {}))
    }, [])

    return <div>
        <input ref={e => refs.current[0] = e} defaultValue={0}/>
        <input ref={e => refs.current[1] = e} defaultValue={0}/>
        <span>:</span>
        <input ref={e => refs.current[2] = e} defaultValue={0}/>
        <input ref={e => refs.current[3] = e} defaultValue={0}/>
    </div>
}

export default Time

How can I convert commented out code to use date-fns ?

You'll have to do the porting work, there's no universal way around it. Although it's reasonably easy. For instance, for the part of the code

//     let time = [0, 1, 2, 3].map(i => refs.current[i].value).join('')
//     time = moment(time, 'HHmm').add(i % 2 ? 1 : 10, Math.floor(i / 2) ? 'm' : 'h').format('HHmm').split('');
//     [0, 1, 2, 3].forEach(i => refs.current[i].value = time[i])

it would be

let time = [0, 1, 2, 3].map(i => refs.current[i].value).join('')
const parsed = parse(time, "HHmm");
console.log("parsed", parsed)
const addF = Math.floor(i / 2) ? addMinutes : addHours;
const added = addF(parsed, i % 2 ? 1 : 10);
const formatted = format(added, "HHmm"); 
time = formatted.split('');
[0, 1, 2, 3].forEach(i => refs.current[i].value = time[i])

code is split into assignments for readability, but you could just chain the functions with lodash compose + date-fns/fp if you'd prefer oneliners.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM