用JS實現根據當前時間隨機生成流水號或者訂單號


既然是根據當前的時間,那么我們第一步就是獲取當前時間,然后拼接一個隨機數,這樣便實現了隨機生成一個流水號,注意由於要求每個流水號都是獨一無二的,生成后提交的過程中還是需要驗證當前的流水號是否存在,如果存在則需要提示用戶或者再次生成。

//      根據當前時間和隨機數生成流水號
      randomNumber() {
        const now = new Date()
        let month = now.getMonth() + 1
        let day = now.getDate()
        let hour = now.getHours()
        let minutes = now.getMinutes()
        let seconds = now.getSeconds()
        month = this.setTimeDateFmt(month)
        hour = this.setTimeDateFmt(hour)
        minutes = this.setTimeDateFmt(minutes)
        seconds = this.setTimeDateFmt(seconds)
        return now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 23 + 100)).toString()
      }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM