JS to manipulate dates

Asked

Viewed 79 times

-3

how do I create a js that displays the contents of the array according to the days of the week, type I have the following array:

{
    "days": {
        "monday": [
            {"message": "Olá"}
        ],
        "tuesday": [
            {"message": "Seja bem vindo"}
           
        ]
    }
}

I want to know if with js it is possible to display the content "Monday" on Monday and "Tuesday" on Tuesday and so on. It is possible to do this?

1 answer

2


With the Date object you can get the day of the week according to the date, and with this information, you can display a custom message. Here is an example:

<!DOCTYPE html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>JavaScript: Objeto Data</title>
  <script>
    var now  = new Date();        // Data atual
    var hrs  = now.getHours();    // 0 a 23
    var mins = now.getMinutes();
    var secs = now.getSeconds();
	
	
	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 day = weekday[now.getDay()];


    document.writeln("<p>Data de Hoje: " + now + "</p>");
    document.writeln("<p>Hora: " + hrs + "</p>");
    document.writeln("<p>Minutos: " + mins + "</p>");
    document.writeln("<p>Segundos:" + secs + "</p>");
	document.writeln("<p>Hoje é " + day + "</p>");
    
	document.writeln("<h2>" + day  + "</h2>");
    
  </script>
</head>
<body></body>
</html>

  • You’ve helped me so much, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.