本文主要两种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
来源:51CTO
作者:吴维炜
链接:https://blog.csdn.net/qq_38209578/article/details/100654560