check whether the date entered by the user is current date or the future date

前端 未结 5 543
离开以前
离开以前 2020-12-18 19:50

I was browsing through the net to find a javascript function which can check whether the date entered by the user is current date or the future date but i didn\'t found a s

5条回答
  •  别那么骄傲
    2020-12-18 20:15

    ES6 version with tolerable future option.

    I made this little function that allows for some wiggle room (incase data coming in is from a slightly fast clock for example).

    It takes a Date object and toleranceMillis which is the number of seconds into the future that is acceptable (defaults to 0).

    const isDistantFuture = (date, toleranceMillis = 0) => {
        // number of milliseconds tolerance (i.e. 60000 == one minute)
        return date.getTime() > Date.now() + toleranceMillis
    }
    

提交回复
热议问题