Home  >  Article  >  Web Front-end  >  How to use JS to randomly generate serial numbers based on the current time

How to use JS to randomly generate serial numbers based on the current time

php中世界最好的语言
php中世界最好的语言Original
2018-06-01 09:45:022173browse

This time I will show you how to operate JS to randomly generate a serial number based on the current time, and how to operate JS to randomly generate a serial number based on the current time. What are the precautions?. Here is a practical case. Let’s take a look. take a look.

Since it is based on the current time, our first step is to obtain the current time and then splice a random number. This will achieve a random generation of a serial number. Note that each serial number is required to be unique. , during the submission process after generation, it is still necessary to verify whether the current serial number exists. If it exists, the user needs to be prompted or generated again.

//   根据当前时间和随机数生成流水号
   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() * 89 + 100)).toString()
   }

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS reflection and dependency injection use case analysis

How to develop verification codes in WeChat mini programs Password input box function

The above is the detailed content of How to use JS to randomly generate serial numbers based on the current time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn