Home > Web Front-end > JS Tutorial > body text

How to display today's year and a random 4 digit number at the same time?

WBOY
Release: 2023-08-31 16:53:07
forward
1073 people have browsed it

How to display todays year and a random 4 digit number at the same time?

Let's see "How to display the current year and a random 4-digit number at the same time". This is useful for a variety of reasons, such as creating unique identifiers, tracking dates, or displaying dates in a specific format. By using some simple programming techniques, you can quickly create this effect with minimal coding knowledge. Let's explore how to do this in JavaScript.

Let’s dive into this article to get a better understanding of displaying today’s year and random 4 digits at the same time.

Use JavaScript random() method

Math.random() The static method provides a floating point pseudo-random number that is roughly evenly distributed in the range, greater than or equal to 0 and less than 1, you can use it to extend your desired range. The initial seed of the random number generation algorithm is chosen by the implementation; it cannot be changed or reset by the user.

grammar

The following is the syntax of random() -

Math.random()
Copy after login

Example

In the example below, we use the random() method to run the script to generate random numbers and the getFullYear() method to return the 4-digit year.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         const x = new Date();
         let year =x.getFullYear();
         document.getElementById("tutorial").innerHTML ='Todays year:'+ year;
         var val = Math.floor(1000 + Math.random() * 9000);
         document.write('Random 4 Digit number: '+val);
      </script>
   </body>
</html>
Copy after login

When the script executes, it will generate an output containing today's year and the 4-digit random number displayed on the web page. As long as the user executes the script, the random number will keep changing.

Using JavaScript slice() method

slice()The method returns a shallow copy of a subset of the array, where start and end are the indices of the array items, and the subset is selected from start to end (end is not inclusive). The initial array does not change.

grammar

The following is the syntax of slice() -

array.slice(start, end)
Copy after login

Example

Consider the following example where we use the slice() method and the random() method to get today's year along with a 4-digit random number.

<!DOCTYPE html>
<html>
   <body>
      <input type="text" id="tutorial" value="" disabled />
      <script>
         const now = new Date();
         let randomNum = '';
         randomNum += Math.round(Math.random() * 9);
         randomNum += Math.round(Math.random() * 9);
         randomNum += now.getTime().toString().slice(-2);
         document.getElementById("tutorial").value = `${new Date().getFullYear()}${randomNum}`;
      </script>
   </body>
</html>
Copy after login

When running the above script, an output window will pop up showing a disabled input field containing the four-digit today's year and the four-digit random number displayed on the web page. As long as the user executes the script, the random number will keep changing.

The above is the detailed content of How to display today's year and a random 4 digit number at the same time?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!