How To Remove The Seconds And Milliseconds From A Date In JavaScript

Remove the Seconds and Milliseconds from a Date in JavaScript

This guide can help you learn how to remove the Seconds and Milliseconds from a Date in JavaScript. Follow this guide to learn more about it with the explanation and examples below.

Remove The Seconds And Milliseconds From A Date In JavaScript

These are some solutions that can help you remove the Seconds and Milliseconds from a Date in JavaScript. To do that, you can get the specified date and time except for seconds and milliseconds. Another solution is handling the String whose value is date and time. Let’s learn about it in the next title below.

Get the specified date and time

To remove the seconds and milliseconds from a Date, you can create a new Date that doesn’t have the seconds and milliseconds attribute.

Look at the example below to learn more about this solution.

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Remove The Seconds And Milliseconds From A Date In JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: rebeccapurple;">
        How To Remove The Seconds And Milliseconds From A Date In JavaScript
    </h1>
    <h2 style="color:black;">
        Remove The Seconds And Milliseconds From A Date In JavaScript
    </h2>
    <h3: style="color: black;">
         Solution 1: Get the specified date and time.
    </h3>
    <script>
        document.write("<br>The current time: ");

        /*  Create a new Date object by keyword 'new'. It will return the date and time now. */
        const date = new Date();

        document.write(date.toLocaleString())

        function updatedDateAndTime(date = new Date()){

            // Create a date that doesn't have the seconds and milliseconds attribute.
            updatedDate =  new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes())
            return updatedDate;
        }

        document.write("<br>The updated time: "+updatedDateAndTime(date).toLocaleString())

    </script>
</body>
</html

Output

Handle the date and time String

First, you get the time as a String by the toLocaleTimeString() method.

Second, you change the seconds to 0.

Match the String of the time and the String of the date. You will get the result.

Look at the example below to learn more about this solution.

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Remove The Seconds And Milliseconds From A Date In JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: rebeccapurple;">
        How To Remove The Seconds And Milliseconds From A Date In JavaScript
    </h1>
    <h2 style="color:black;">
        Remove The Seconds And Milliseconds From A Date In JavaScript
    </h2>
    <h3: style="color: black;">
         Solution 2: Handle the date and time String.
    </h3>
    <script>
        document.write("<br>The current time: ");

        /* Create a new Date object by keyword 'new'. It will return the date and time now. */
        const date = new Date();

        document.write(date.toLocaleString())
        
        function updatedDateAndTime(date ) {
            let time = date.toLocaleTimeString();

            // Update the seconds to a value as 0.
            let updatedTime = time.slice(0,time.length-2)+"00";

            // Match the new time and the current date
            let result = updatedTime+", "+date.toLocaleDateString();
            return result;
        }

        document.write("<br>The updated time: "+updatedDateAndTime(date))
    </script>
</body>
</html

Output

Summary

To remove the Seconds and Milliseconds from a Date in JavaScript, you can create a new date by getting the specified date and time except for seconds and milliseconds or handling the date and time String. Choose the solution that is best for you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *