1

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

  • You should really use a map or array instead of this. Store all the strings you want to display in it, and then use the hour to retrieve the appropriate one from the list: `var list = {}; list[1] = list[2] = "Club Life"; /* ... */ document.write(list[hr]);` – Domino May 07 '17 at 13:19

4 Answers4

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

Divya Mamgai
  • 148
  • 10
Osama
  • 2,912
  • 1
  • 12
  • 15
  • Hi, so I used this code, but it still not work. It bases only on hour, but not hour and day. Do you know what can I change? Thank You – Rokas Šimas May 07 '17 at 16:17
  • how you see that it is not working for me it is working but today is sunday the validation is from Monday to Thursday or Monday only – Osama May 07 '17 at 16:46
  • Rock Rush is shown. It should not be, because it is not Monday Tuesday or, Wednesday – Rokas Šimas May 07 '17 at 17:11
  • Hi my friend it is not the case , I check that it is not shown – Osama May 07 '17 at 20:26
  • Hi aigain, but if you just take and change Spinnin Sessions time to what it is and week day something like to Friday, you will see at text will appear, even if it is not Friday. If you just take a look what I made: https://www.w3schools.com/code/tryit.asp?filename=FFDHV2UNROQF – Rokas Šimas May 07 '17 at 21:14
  • Dear if you set n="Friday" in the if condition of the spinnin session it will appear on Friday only – Osama May 07 '17 at 21:18
  • Hi I'm this example I convert condition to n==Tuesday and hr==0 the planet perfecto text appear and this is the condition right only this hr from this day – Osama May 07 '17 at 21:23
  • Thank You so much. You helped me a lot. Have a nice day or maybe night! – Rokas Šimas May 07 '17 at 21:53
  • Why not combine all the if loops into a single one using `else if` construct. This will save precious CPU cycles as subsequent conditions will not be validated. – Divya Mamgai May 08 '17 at 06:15
  • I also recommended this idea, but as it is a simple and short code no problem here but for large and complicated one it is the best. – Osama May 08 '17 at 06:27
  • Hi again so I should use this code? I used this yesterday and it worked now it do not. I have also change something with mistake: – Rokas Šimas May 08 '17 at 15:44
  • it is better to do that – Osama May 08 '17 at 15:47
  • But it seems not to work because someone edit and this function do not work for me. Yours worked well – Rokas Šimas May 08 '17 at 15:51
1

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

Community
  • 1
  • 1
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
  • 2
    Why use `new Array(7)`? When we can simply instantiate using `["Sunday", "Monday", ...]`. – Divya Mamgai May 07 '17 at 12:55
  • 1
    This is just an example copied from the link I added. It helps demonstrate the use of `getDay` function. It also makes it more noticeable that the first day is Sunday and it's value is 0. – Itay Gal May 07 '17 at 12:56
  • @DivyaMamgai Probably because of [Java](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Vector.html). –  May 07 '17 at 12:58
  • @ItayGal Yes, I understand that, but I just wanted to point that this might not be the best practice. Sorry if I'm being rude here. – Divya Mamgai May 07 '17 at 13:05
  • You're not being rude. It makes little sense to use the Array constructor here. – Domino May 07 '17 at 13:11
  • @JacqueGoupil Please give me a reason as to why it may be more beneficial to use Array constructor. – Divya Mamgai May 07 '17 at 15:55
  • But how do I make that it would be both shown on decided weekday and hour of weekday. Thanks – Rokas Šimas May 07 '17 at 17:49
  • @DivyaMamgai Oh, I'm agreeing with you. `new Array()` is using the constructor while `[...]` is a literal. I'd use a literal because there's no reason not to, it's shorter and looks better. – Domino May 08 '17 at 02:54
1

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

brk
  • 48,835
  • 10
  • 56
  • 78
0

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