Reputation:
I have the following code to display a message daily,
<!doctype html>
<html>
<head>
<script type="text/javascript">
var msg = new Array();
Stamp = new Date();
today = Stamp.getDate();
msg[1] = "msg 1";
msg[2] = "msg 2";
msg[3] = "msg 3";
msg[4] = "msg 4";
msg[5] = "msg 5";
msg[6] = "msg 6";
msg[7] = "msg 7";
msg[8] = "msg 8";
msg[9] = "msg 9";
msg[10] = "msg 10";
msg[11] = "msg 11";
msg[12] = "msg 12";
msg[13] = "msg 13";
msg[14] = "msg 14";
msg[15] = "msg 15";
msg[16] = "msg 16";
msg[17] = "msg 17";
msg[18] = "msg 18";
msg[19] = "msg 19";
msg[20] = "msg 20";
msg[21] = "msg 21";
msg[22] = "msg 22";
msg[23] = "msg 23";
msg[24] = "msg 24";
msg[25] = "msg 25";
msg[26] = "msg 26";
msg[27] = "msg 27";
msg[28] = "msg 28";
msg[29] = "msg 29";
msg[30] = "msg 30";
msg[31] = "msg 31";
msg[32] = "msg 32";
msg[33] = "msg 33";
msg[34] = "msg 34";
.
.
.
.
msg[6000] = "end msg";
function writeMessage() {
document.write(msg[today]);
}
</script>
</head>
<body>
<strong>Daily Message:</strong>
<script>
writeMessage();
</script>
</body>
It displays message for all the days and then starts from first for the next month. What i want is to continue to display the next message, for example msg 32 should be displayed in the next month and so on till I come to the end message. Could you help me in modifying my code logic to achieve what I want?
Upvotes: 3
Views: 2081
Reputation: 1633
Changed my answer around as per suggestions, I believe this version works. Taken from reference here
Used alert(today); to call the day value from popup to confirm it worked:
<!doctype html>
<html>
<head>
<script type="text/javascript">
var msg = new Array();
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var today = Math.floor(diff / oneDay);
msg[1] = "msg 1";
msg[2] = "msg 2";
msg[3] = "msg 3";
msg[4] = "msg 4";
msg[5] = "msg 5";
msg[6] = "msg 6";
msg[7] = "msg 7";
msg[8] = "msg 8";
msg[9] = "msg 9";
msg[10] = "msg 10";
msg[11] = "msg 11";
msg[12] = "msg 12";
msg[13] = "msg 13";
msg[14] = "msg 14";
msg[15] = "msg 15";
msg[16] = "msg 16";
msg[17] = "msg 17";
msg[18] = "msg 18";
msg[19] = "msg 19";
msg[20] = "msg 20";
msg[21] = "msg 21";
msg[22] = "msg 22";
msg[23] = "msg 23";
msg[24] = "msg 24";
msg[25] = "msg 25";
msg[26] = "msg 26";
msg[27] = "msg 27";
msg[28] = "msg 28";
msg[29] = "msg 29";
msg[30] = "msg 30";
msg[31] = "msg 31";
msg[32] = "msg 32";
msg[33] = "msg 33";
msg[34] = "msg 34";
.
.
.
.
msg[6000] = "end msg";
function writeMessage() {
document.write(msg[today]);
}
</script>
</head>
<body>
<strong>Daily Message:</strong>
<script>
writeMessage();
</script>
</body>
Upvotes: 1
Reputation: 4223
Have a 2d Array for your messages where the first index is the month and the second index is the day.
var msg = new Array(12);
msg[0] = new Array(31); // January
msg[1] = new Array(28); // February
...
msg[0][0] = "January 1st's message";
msg[0][1] = "January 2nd's message";
...
msg[0][30] = "January 31st's message";
msg[1][0] = "February 1st's message";
...
month = stamp.getMonth();
today = Stamp.getDate() - 1;
function writeMessage() {
document.write(msg[month][today]);
}
Upvotes: 1
Reputation: 97237
If it doesn't matter where you start in your array, you can do something like this:
var daysSinceEpoch = Math.floor(new Date().getTime() / (24 * 60 * 60 * 1000));
var index = daysSinceEpoch % msg.length;
document.write(msg[index]);
If you have a fixed start date, you can do:
var startDate = ... // initialize your startDate
var daysSinceStart = Math.floor((new Date().getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
var index = daysSinceStart % msg.length;
document.write(msg[index]);
Upvotes: 3
Reputation: 833
I think you should have a "START" date. and then calculate the days between "START" date and "today". then use the "days" you just calculated as the array index to display your message.
Upvotes: 1