JavaScript code to achieve a simple calendar effect

JavaScript code to achieve a simple calendar effect

This article shares the specific code for JavaScript to achieve a simple calendar effect for your reference. The specific content is as follows

The effect is as follows:

Code:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
  * {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
  }
  
  #box {
   width: 400px;
   height: 502px;
   border: 2px solid orange;
   margin: 50px auto;
   font-size: 48px;
   text-align: center;
  }
  
  #ym {
   height: 100px;
   line-height: 100px;
  }
  
  #d {
   height: 200px;
   line-height: 200px;
   background-color: orange;
   font-size: 72px;
  }
  
  #w {
   height: 100px;
   line-height: 100px;
  }
  
  #hms {
   height: 100px;
   line-height: 100px;
  }
 </style>
</head>

<body>
 <div id="box">
  <div id="ym"></div>
  <div id="d"></div>
  <div id="w"></div>
  <div id="hms"></div>

 </div>
 <script>
  //Call the getDateAndTime method getDateAndTime();

  //Start the timer and call the getDateAndTime method window.setInterval(getDateAndTime, 1000);

  function getDateAndTime() {


   //Get the current date and time of the system var date = new Date();

   //Extract the elements of date and time var year = date.getFullYear();
   var month = date.getMonth() + 1;
   var day = date.getDate();
   var week = date.getDay();
   var hour = date.getHours();
   var minute = date.getMinutes();
   var second = date.getSeconds();

   //Process the week format switch (week) {
    case 0:
     week = 'Sunday';
     break;
    case 1:
     week = 'Monday';
     break;
    case 2:
     week = 'Tuesday';
     break;
    case 3:
     week = 'Wednesday';
     break;
    case 4:
     week = 'Thursday';
     break;
    case 5:
     week = 'Friday';
     break;
    case 6:
     week = 'Saturday';
     break;


   }
   //Define function to process the format of hours, minutes and seconds function formatHMS(time) {
    if (time < 10) {
     return '0' + time;
    } else {
     return time;
    }
   }

   //Get the page element var ym = document.getElementById('ym');
   var d = document.getElementById('d');
   var w = document.getElementById('w');
   var hms = document.getElementById('hms');
   //Write the date and time to the page ym.innerHTML = year + 'year' + month + 'month';
   d.innerHTML = day;
   w.innerHTML = week;
   hms.innerHTML = hour + 'hour' + minute + 'minute' + second + 'second';

  }
 </script>

</body>

</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js css+html to realize a simple calendar
  • A guide to using calendar.js, a lightweight native js calendar plugin
  • JS learning a simple calendar control
  • Vue.js creates Calendar effect
  • JS Calendar Recommendation
  • Pure js simple calendar implementation code
  • js writes a simple calendar effect for the day [implementation code]
  • js calendar control (accurate to the minute)
  • PHP+javascript calendar control
  • Simple JS calendar control example code

<<:  Solution to MySql Error 1698 (28000)

>>:  MySQL password modification example detailed explanation

Recommend

jQuery Ajax chatbot implementation case study

Chatbots can save a lot of manual work and can be...

Difference between varchar and char types in MySQL

Table of contents aforementioned VARCHAR Type VAR...

Layui implements the login interface verification code

This article example shares the specific code of ...

Three BOM objects in JavaScript

Table of contents 1. Location Object 1. URL 2. Pr...

Detailed explanation of small state management based on React Hooks

Table of contents Implementing state sharing base...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

How to connect Django 2.2 to MySQL database

1. The error information reported when running th...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

Summary of some HTML code writing style suggestions

Omit the protocol of the resource file It is reco...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...