Ritim Ä°stanbul A5 Blok No: 46, D: 112, 34846 Maltepe/Ä°stanbul

How to add the current date automatically to HTML with JavaScript?

In web development, it’s often necessary to display the current date on a webpage. Whether you’re building a blog, an e-commerce site, or a dashboard, showing the date can be both informative and engaging for your users. In this blog post, we’ll explore how to add the current date to your HTML using JavaScript. We’ll provide you with a step-by-step guide and a ready-to-use code example.

Demo data table with the current date added to an HTML page using JavaScript
Demo data table with the current date added to an HTML page using JavaScript

Incorporating automation into your web development projects, as demonstrated in this guide, underscores the power and significance of streamlining repetitive tasks. Automatically adding the current date to your HTML using JavaScript is not only a practical feature for your website but also a testament to how automation can save time and reduce the risk of human error. Automation empowers developers to deliver a seamless user experience, ensuring that visitors always see the most up-to-date information without manual intervention. It’s a reminder that in the digital age, automation is not just a convenience, but a vital tool for enhancing efficiency and reliability in web development.

Step 1: Create Your HTML Structure

First, you need an HTML element where you want to display the current date. For this example, let’s say you have a <figure> element with a <table> inside, and you want to add the date to the first column of the table.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <figure class="fb-product-prices-table">
        <table>
            <tr>
                <th>Title 1</th>
                <th>Title 2</th>
                <th>Title 3</th>
                <th>Title 4</th>
            </tr>
            <tr>
                <td>Data 1</td>
                <td>Other data in the first row</td>
                <td>More data</td>
                <td>And more data</td>
            </tr>
            <tr>
                <td>Data 2</td>
                <td>Other data in the second row</td>
                <td>More data</td>
                <td>And more data</td>
            </tr>
            <tr>
                <td>Data 3</td>
                <td>Other data in the third row</td>
                <td>More data</td>
                <td>And more data</td>
            </tr>
        </table>
    </figure>
</body>
<style>
    table {
        border-collapse: collapse;
    }
    th, td {
        border: 1px solid black;
        padding: 5px;
    }
</style>
</html>

Step 2: Write the JavaScript Code

Now, let’s write the JavaScript code that will automatically insert the current date into the first column of the table.

// Get the current date
const currentDate = new Date();
const day = currentDate.getDate().toString().padStart(2, '0');
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const year = currentDate.getFullYear();
const formattedDate = `${day}/${month}/${year}`;

// Find the "fb-product-prices-table" element
const tableContainer = document.querySelector(".fb-product-prices-table");

if (tableContainer) {
  // Find the <table> element inside it
  const table = tableContainer.querySelector("table");

  if (table) {
    // Select all <td> elements in the first column and set the current date
    const tdElements = table.querySelectorAll("td:first-child");

    tdElements.forEach((td) => {
      td.textContent = formattedDate;
    });
  }
}

Step 3: Implement the Code

Now that you have the HTML structure and JavaScript code ready, you can add them to your webpage. Include the JavaScript code inside a <script> tag, typically at the bottom of your HTML file, just before the closing </body> tag.

<script>
  // JavaScript code here
</script>

Once you’ve added the code, make sure to save your HTML file and open it in a web browser to see the current date automatically inserted into the first column of your table.

Conclusion

Adding the current date to your HTML using JavaScript is a useful feature for various web development projects. In this blog post, we provided you with a simple step-by-step guide and a ready-to-use code example to help you achieve this. You can adapt this code to your specific needs, whether you’re working on a blog, e-commerce site, or any other web application that requires displaying the current date.

More blogs

JavaScript var vs let variables: What’s the difference?

JavaScript is a popular and versatile programming language that powers many web applications. However, it also has some quirks and pitfalls that can confuse...

What ‘data’ element used for in HTML?

HTML is a markup language that allows us to create web pages with different types of content. However, not all content is equally understandable...

Is it possible to connect Google Sheets with JavaScript?

The fusion of web development and data management has become crucial for efficient workflow and innovative solutions. One such powerful combination is the integration...

Automatically inserting current dates into HTML elements with JavaScript

In today's blog post, we're going to explore a useful JavaScript code snippet that allows you to automatically insert the current date into specific...

What is Swiper JS and how to use it?

Swiper JS is a powerful and versatile library that allows you to create stunning and interactive web pages and apps with touch-friendly sliders, carousels,...

Splitting paragraphs into sentences in Google Sheets with a custom function

Google Sheets sometimes lacks the precise functions we require. However, the application offers a remedy in the form of "Apps Script," enabling the creation...

More from the blog

JavaScript var vs let variables: What’s the difference?

JavaScript is a popular and versatile programming language that powers many web applications. However, it also has some quirks and pitfalls that can confuse...

What ‘data’ element used for in HTML?

HTML is a markup language that allows us to create web pages with different types of content. However, not all content is equally understandable...

Is it possible to connect Google Sheets with JavaScript?

The fusion of web development and data management has become crucial for efficient workflow and innovative solutions. One such powerful combination is the integration...

Automatically inserting current dates into HTML elements with JavaScript

In today's blog post, we're going to explore a useful JavaScript code snippet that allows you to automatically insert the current date into specific...