Skip to content Skip to sidebar Skip to footer

How To Implement Logic Like "tip Of The Day" Dynamically Every Day 24 Hrs In Asp.net

I have a table 'Tips' which has several rows. At the home page I need to display a random single tip as 'Tip of the day'. After 24 hours dynamically I should display a random tip f

Solution 1:

You can e.g. generate the SHA-1 hash of the current date and then compute its remainder on the modulo of the total tips count, which will get you the number of the "Tip of the day".

For example,

TipOfTheDay[] tips;

var now = DateTime.Now;
var buffer = newbyte[] { (byte)(now.Year % 256), (byte)(now.Year / 256), (byte)now.Month, (byte)now.Day };
SHA1 encoder = new SHA1CryptoServiceProvider(); 
var hash = encoder.ComputeHash(data);
int tipNumber = 0;
for(var b in hash) {
    tipNumber += b;
    tipNumber %= tips.Count;
}
return tips[tipNumber];

This will return you a new (pseudo-random) tip every day, and the tip will remain the same during the day.

Solution 2:

If you are using Asp.Net MVC you can use output cache attribte and set the duration as 24 hr.

[OutputCache(Duration=86400)]

for asp.net, you can add <%@ OutputCache duration="86400" varybyparam="None" %> on the control view

Post a Comment for "How To Implement Logic Like "tip Of The Day" Dynamically Every Day 24 Hrs In Asp.net"