Rokas Šimas
Rokas Šimas

Reputation: 43

text depend on time of day and day of week

So I have this code that shows different text depend on the time of the day.

</style>

<!-- HTML Code -->
<strong class="GeneratedText"><script type="text/javascript">
document.write("<p>");
var day = new Date();
var hr = day.getHours();
 if (hr == 1) {
document.write("    Club Life"    );
 }
 if (hr == 2) {
document.write("    Club Life    ");
 }
 if (hr == 3) {
document.write("    Identity     ");
 }
 if (hr == 4) {
document.write("A State Of Trance");
 }
 if (hr == 5) {
document.write("A State Of Trance");
 }
 if ((hr == 6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10)               || (hr == 11) || (hr == 12) || (hr == 13) || (hr == 14)) {
document.write("    New Musikk"   );
 }
 if (hr == 15) {
document.write("     Protocol"    );
 }
 if ((hr == 16) || (hr == 17) || (hr == 18) || (hr == 19)) {
document.write("    New Musikk"   );
 }
if ((hr == 20) || (hr == 21)) {
document.write("    Rock Rush"    );
 }
if (hr == 22){
document.write(" Spinnin Sessions");
 }
 if (hr == 23) {
document.write(" Planet Perfecto" );   
  }
 if (hr==0) {
document.write(" Planet Perfecto" );
}
document.write("</p>");
</script></strong>

Problem is that it show the same text every day. For example i want to that Spinnin Sessions only should be shown on Mondays, and Rock Rush only from Monday to Thursday. And same think with all of them. (different text in different day. How can I do that? Another problem is that text is shown based on viewers time, but not mine. How can this be fixed? Thank You

Upvotes: 1

Views: 1696

Answers (4)

Osama
Osama

Reputation: 3040

<strong class="GeneratedText">
    <script type="text/javascript">
        document.write("<p>");
        var day = new Date();
        var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        var d = weekday[day.getDay()];
        var hr = day.getHours();
        if (hr == 1) {
            document.write("    Club Life");
        } else if (hr == 2) {
            document.write("    Club Life    ");
        } else if (hr == 3) {
            document.write("    Identity     ");
        } else if (hr == 4) {
            document.write("A State Of Trance");
        } else if (hr == 5) {
            document.write("A State Of Trance");
        } else if ((hr == 6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11) || (hr == 12) || (hr == 13) || (hr == 14)) {
            document.write("    New Musikk");
        } else if (hr == 15) {
            document.write("     Protocol");
        } else if ((hr == 16) || (hr == 17) || (hr == 18) || (hr == 19)) {
            document.write("    New Musikk");
        } else if (((hr == 20) || (hr == 21)) &&
            ( d == "Monday" || d == "Tuesday" || d == "Wednesday" || d == "Thursday")) {
            document.write("    Rock Rush");
        } else if (hr == 22 && d == "Monday") {
            document.write(" Spinnin Sessions");
        } else if (hr == 23) {
            document.write(" Planet Perfecto");
        } else if (hr == 0) {
            document.write(" Planet Perfecto");
        }
        document.write("</p>");
    </script>
</strong>

the second question required to get time from server using PHP or ASP.NET for example

Upvotes: 3

brk
brk

Reputation: 50291

day.getHours() will give the hour ,to get the day use getDay which will return the day of the week in integer , where 0 represent sunday.

Date object return the specified date according to local time set in the system. To solve the second problem, use the server date

Upvotes: 1

user2949702
user2949702

Reputation:

The Date object has a method getDay.

var Xmas95 = new Date('December 25, 1995 23:15:30');
var weekday = Xmas95.getDay();
console.log(weekday); // 1

Upvotes: 0

Itay Gal
Itay Gal

Reputation: 10834

You can use getDay() function to get the day of the week.

Here's an example

var d = new Date();
var weekday = new Array(7);
weekday[0] =  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

Regarding the time, check this out

Upvotes: 1

Related Questions