JS生成订单号

匿名 (未验证) 提交于 2019-12-02 23:59:01

本文主要两种js实现订单号的生成。

一、时间戳+6位随机数的订单号

function orderCode() {   var orderCode='';   for (var i = 0; i < 6; i++) //6位随机数,用以加在时间戳后面。   {     orderCode += Math.floor(Math.random() * 10);   }   orderCode = new Date().getTime() + orderCode;  //时间戳,用来生成订单号。   console.log(orderCode)   return orderCode; }

结果如下:

1567996498547206650

二、日期+6位随机数的订单号

function setTimeDateFmt(s) {  // 个位数补齐十位数   return s < 10 ? '0' + s : s; }  function 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 = setTimeDateFmt(month)   day = setTimeDateFmt(day)   hour = setTimeDateFmt(hour)   minutes = setTimeDateFmt(minutes)   seconds = setTimeDateFmt(seconds)   let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();   console.log(orderCode)   return orderCode; }

结果如下:

20190909103109582536
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!