Home > Mobile >  How to Generate a Sequence of AlphaNumberic using JavaScript?
How to Generate a Sequence of AlphaNumberic using JavaScript?

Time:08-06

i'm a newbie in javascript. As i need to generate a sequence of alphanumeric as AB22231000. The sequence should change every month as AB22241000. So the number 3 should change to 4 & 4 to 5, so on. The number should increment by 1 after every month. So anyone could please help. Thank you in advance.

CodePudding user response:

I'm not sure that I understand you but I think that should help.

// sequence goes here 'AB' a character, after that 2022 the year, then month , then 000
// example: 'AB'   2022   1   000 = 'AB202201000'
const createSequence = () => {
  var month = date.getMonth();
  var year = date.getFullYear();
  var sequence = 'AB'   year   month   '000';
};

CodePudding user response:

Well, the above answer is correct but after reading your conversion, maybe I am wrong but I guess this is what you want -

const createSequence = () => {
  const date = new Date();
  let day = date.getDay();
  let month = date.getMonth();
  let year = date.getFullYear();
  let sequence = 'AB'   year.toString().replace('0','')   month   '000' day;
// this will give AB22270005
  console.log(sequence);
};
createSequence();

The above code will give AB22270005 here and 7 is the month so it will increase by 1 every month and 5 is the date of the day so it will also increase by one.

  • Related