微信小程序 - 随机生成订单号(JavaScript)

前言

如题,随机生成订单号是很常见的需求,如下图所示:
在这里插入图片描述

第一种

可自己拼接其他字母。

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

function orderCode()
{
    
    
   	// 存放订单号
    let orderCode = '';
    
    // 6位随机数(加在时间戳后面)
    for (var i = 0; i < 6; i++)
    {
    
    
      orderCode += Math.floor(Math.random() * 10);
    }

    // 时间戳(用来生成订单号)
    orderCode = 'D' + new Date().getTime() + orderCode;
	
	// 打印
    console.log(orderCode)// D1601545805958923658
}

第二种

日期 + 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

猜你喜欢

转载自blog.csdn.net/weixin_44198965/article/details/108896297
今日推荐