Javascript: How to do a random based on time?

1

How to add value to text?

And how do you make it stay in this format? for example: ADQ2018060601513610 (20 characters)

Example:

function insertNewSigCdAdq() {
    var pkchaves = document.getElementById("txtPkChaves").style;
    pkchaves.value = ("ADQ" + (Math.random() * 10000000000));
    console.log(pkchaves.value);
}
<input id="txtPkChaves" class="txtPkChaves_adq" name="name_txt" style=" width:100px;" type="text"><br>
<button onclick="insertNewSigCdAdq()"> Teste </button>
    
asked by anonymous 06.06.2018 / 20:25

2 answers

4

Use the toISOString method, and then remove the non-numbers: link

var data = new Date();
console.log(data.toISOString());
console.log(data.toISOString().replace(/[^0-9]+/g, ''));
console.log("ADQ" + data.toISOString().replace(/[^0-9]+/g, ''));
    
06.06.2018 / 20:44
2

Generated a random 2-digit number and concatenated the formatted date, as shown by its example:

var d = new Date();
var nAleatorio = (Math.random() * 89)+10
var nAleatorio = nAleatorio .toFixed(0).split('.');

console.log('Número aleatório: '+ nAleatorio);
console.log('Data formatada: '+d.toDate('yyyymmddhhiiss'));
console.log('ADQ'+d.toDate('yyyymmddhhiiss')+ nAleatorio);

   
<script src="https://rawgit.com/Lautert/helpers/master/javascript.helpers.js"></script>
    
06.06.2018 / 20:55