Display post per day daily until end of post using HTML and Javascript

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 a message for all days and then starts on the first for the next month. I want to continue displaying the next message, for example msg 32 message should be displayed next month and so on until I get to the end of the message. Could you please help me on modifying my code logic to achieve what I want?

+3


source to share


4 answers


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]);

      

+3


source


I think you should have a START date. and then calculate the days between "START" and "today". then use the "days" you just counted as the array index to display your message.



+1


source


Modified my answer as per suggestions, I believe this version works. Taken from the link here

Firewall (today); to call the day value from the popup to confirm it works:

enter image description here

<!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>

      

+1


source


Have a 2d array for posts 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 message";
msg[0][1] = "January 2nd message";
...
msg[0][30] = "January 31st message";
msg[1][0] = "February 1st message";
...

month = stamp.getMonth();
today = Stamp.getDate() - 1;

function writeMessage() { 
    document.write(msg[month][today]);
}

      

+1


source







All Articles