How to set up an automatic Quote of the Day?

I am responsible for the site, and I have created a Quote of the Day, which is currently quite simplified. See here (to the right of the page)

Currently, it receives the Day of the month and month and normalizes to one, and then multiplies by the number of quotes (stored in an XML file) and is rounded down. Although this method will give me the same quote no matter what I was on the machine (something a random number generator could never do), I was told that this method is wrong. If you consider January, then the first pair of quotes will be the same, 1 * 1/360, 2 * 1/360, 3 * 1/360, so the quote is not unique.

Can anyone think of a better way to choose a quote of the day?

+3
source share
6 answers

Funny question. Instead of relying on the days of the month, why not count the days from a specific date? JS provides a pretty good property for this: getTime()which gives you milliseconds from 12:00 UTC on January 1, 1970, which you can convert to days with some simple division.

, , , , ( ?), . , JS , getTimezoneOffset(), , UTC. , , , , .

:

var intQuoteCount = 51; // The number of quotes in your library
var dtNow = new Date();
var intTZOffset = dtNow.getTimezoneOffset() * 60000; // automatically adjust for user timezone
var intNow = dtNow.getTime() - intTZOffset;
var intDay = Math.floor(intNow / 86400000); // The number of 'local' days since Jan 1, 1970
var intQuoteToDisplay = intDay % intQuoteCount;
+1

, - , " ". ( , ), , . "".

"" , , , , - "yyyymmdd" , , , .

, , PRNG srand(mySeed);, "" rand() ( , mySeed ).

+1

, Julian Day, , . , , , , .

+1

. , , .

, , .

0

360 ? , , 500 . .
: 1 #OfQoutes, " ".
, , , " ", , , . , .
, , , , .

0

<body onLoad="thoughts_authors()">
<script>
function thoughts_authors()
{

var authors=new Array()
authors[0] = "Charles Schulz";
authors[1] = "Jack Wagner";
authors[2] = "Mark Twain";
authors[3] = "Oscar Wilde";
authors[4] = "David Letterman";
authors[5] = "Lily Tomlin";

var thoughts=new Array()
thoughts[0] = "Good Day Is Today";
thoughts[1] = "Style Is What You Choose";
thoughts[2] = "Be The Best Version Of You.";
thoughts[3] = "Truth Along Triumphs.";
thoughts[4] = "How can Life Be Devastating When YOU Are Present in It.";
thoughts[5] = "Believe In What You Say";

index = Math.floor(Math.random() * thoughts.length);

alert(thoughts[index]+ "-" + authors[index]);

}
</script>
Run codeHide result

THIS WILL RECEIVE RANDOM QUOTES IN THE CASE OF RANDOM AUTHORS

0
source

Source: https://habr.com/ru/post/1768075/


All Articles