0

need some logics or algorithm here:

I want to use javascript to implement a simple counter, which adds rand() int every day (ignoring page loads) and stores that number without using any database... It doesn't have so start from 0, but it has to be increasing everyday. Example: 2050. Next day: 2053..and so on.

I am thinking to use javascript date function, as year is increasing and month numbers are increasing (each year), but days set to 1 each month.. So any ideas what kind of algorithm or javascript function I could use to implement this?

It would be perfect to have rand() function, which increments only, but everyday, not every page load.. Probably it's silly question, but it's Saturday and my brain doesnt function anymore.. :) Thank you so much!

Update: getTime() kinda does the thing, but it gives me smth like 8176870165464, there last digits are miliseconds, so they are changing too often..I need to increment +1 or +3 ( or relatively small int) every day..and the final counter to be 4 digits, smth like 2040, not 345346345355

4 Answers 4

2

You can use the current date. To make it increasing, just compute the number of days since a given day in the past.

Sign up to request clarification or add additional context in comments.

2 Comments

ha, that's what I needed ! Complete forgot about computing days since a give date.. Saturday is working on me.. thanks!
I will, waiting for someone to come up with a genius algorithm :) Your solution is simple and works, prob will use it :)
1

If you just need a number that goes up every 24 hours, use the current time. javascript's Date.getTime() returns the number of milliseconds since 1970. This will always be going up, if you only want it to change every 24 hours, just divide the number it returns by 24*60*60*1000. (Note that in the case of leap seconds and daylight savings time, there can be more or less than 24 hours in a day.)

3 Comments

copying the same comment: It gives me smth like 8176870, there last digits are miliseconds, so they are changing too often..I need to increment +1 or +3 ( or relatively small int) every day..sorry if I didnt specify that initially..
If you divide that number by 24*60*60*1000 it will only change once per day.
<script type="text/javascript"> var d=new Date(); document.write(d.getTime()/(24*60*60*1000)); </script> gives me 15032.117803275463
0

Does the number have to be globally unique? Unique per-user? Can it repeat for a user?

Either way, you'd have to store it in a cookie, or store it somewhere on your server. And if it's on your server, then there's no point in having Javascript generate it, just have your server-side language of choice do it.

3 Comments

it can repeat, All users will see the same number per day
You should post these questions as a comment.
Then you definitely don't want Javascript - that's purely client-side. You'd have every single person hitting your site trying to generate a number and get your server to remember it - just generate the number on the server and serve it up with your pages.
0

Does it need to be random? Or just increasing? The following increases by 1 each day:

date = Math.floor(Date().getTime() / (24*60*60*1000));

edit: replaced mod above with a div (or at least, a hack to get a div, since JS doesn't support div)

If it needs to be random (in the sense of unpredictable) you need to do something like this:

nr = nr * 100 + yourrandomfunction(nr,100)

Where yourrandomfunction returns a random number given seed 'nr' and max '100'. But unfortunately, you need to build this yourself:

Seeding the random number generator in Javascript

2 Comments

I like it, but it gives me smth like 8176870, there last digits are miliseconds, so they are changing too often..I need to increment +1 or +3 ( or relatively small int) every day..sorry if I didnt specify that initially..
Heh, stupid error, I used modulo, instead of integer division, I'll fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.