Javascript : How to make an Andom based on time?

Asked

Viewed 65 times

0

Adding value to text?

And how to make it stay in this format? for example: ADQ20180601513610 (with 20 characters)

Example:

function insertNewAdq() {
    var pk = document.getElementById("txtPk").style;
    pk.value = ("ADQ" + (Math.random() * 10000000000));
    console.log(pk.value);
}
<input id="txtPk" class="txtPk_adq" name="name_txt" style=" width:100px;" type="text"><br>
<button onclick="insertNewAdq()"> Teste </button>

  • is an Random or a number based on date/time?

  • a random number based on the date/time/min/sec/millisec

  • The version I made is wrong, I would like to know how to do this other way and how to assign to text

  • wouldn’t be much of a Ranger then, but to generate that number in the format you want, I put in the answer

2 answers

4


  • I get it, thanks for the explanation, I’ll study more on this toString, thank you !!

  • has other methods like the toDateString which can inform the format by region (en-BR, en-US, etc). Already the ISO format is practically what you need, just remove the characters that are not numbers

2

Generated a random number of 2 digits and concatenated the formatted date, as its example, follows:

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.