Reputation: 93
I am trying to create a javascript that displays a message on given dates. In the current script the option of a message per day is used.
Perhaps this is a too complex script for displaying certain dates every two weeks. Can someone help me out with getting the script running?
var days = new Array();
var msgs = new Array();
days[0] = "13 februari 2014";
msgs[0] = "13 februari 2014";
days[1] = "27 februari 2014";
msgs[1] = "27 februari 2014";
days[2] = "6 maart 2014";
msgs[2] = "6 maart 2014";
days[3] = "20 maart 2014";
msgs[3] = "20 maart 2014";
days[4] = "3 april 2014";
msgs[4] = "3 april 2014";
days[5] = "17 april 2014";
msgs[5] = "17 april 2014";
var months = new Array("",
"januari", "februari", "maart", "april", "mei", "juni",
"juli", "augustus", "september", "oktober", "november", "december"
);
var today = new Date();
var mon = months[today.getMonth() + 1];
var day = today.getDate();
var year = today.getYear();
function dateMsg() {
for (i = 0; i < days.length; i++) {
tempdate = new Date(days[i]);
tempmonth = months[tempdate.getMonth() + 1];
tempday = tempdate.getDate();
tempyear = tempdate.getYear();
if (year == tempyear && mon == tempmonth && day == tempday)
return (msgs[i]);
}
return (day + " " + mon + " " + year);
}
document.write('<b>' + dateMsg() + '</b>');
Upvotes: 1
Views: 116
Reputation: 2871
When you create the new date tempdate = new Date(days[i]);
you are not creating a date object because the date constructor does not accept the names for months that you are using.
Also: try replacing all instances of getYear()
with getFullYear()
as getYear()
is deprecated
Here is my example with the month february in english: http://jsfiddle.net/w7jTV/4/
However, if you are just wanting different text one day every 2 weeks there are much simpler ways to do this: http://jsfiddle.net/w7jTV/5/
Upvotes: 1