Warning: session_start(): open(/tmp/sess_c447c6dd9e0f3a2df6a8690427728f96, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Add Hours To A Date Object Using JavaScript - LearnShareIT

How To Add Hours To A Date Object Using JavaScript

add hours to a date object using JavaScript

This guide will help you learn how to add hours to a date object using JavaScript by a method built-in Date object. Let’s follow this tutorial to learn about it with the explanation and examples below.

Add Hours To A Date Object Using JavaScript

These are some solutions that can help you add hours to a date object using JavaScript.

Solution 1: Using setHours() method

This method helps you set the hour of the date that you want.

Syntax:

Date.setHours(hours)

Parameters:

  • hours: The hour of the date you want to set.

Return value:

The number of milliseconds from January 1, 1970 to now.

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

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Add Hours To A Date Object Using JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: rebeccapurple;">
        How To Add Hours To A Date Object Using JavaScript
    </h1>
    <h2 style="color:rgb(52, 150, 206);">
        Add Hours To A Date Object Using JavaScript
    </h2>
    <h3: style="color: black;">
         Solution 1: Use the setHours() method
    </h3>
    <script>
        document.write("<br>The current time: ");
        const date = new Date();
        document.write(date.toLocaleTimeString()+"<br>") // print the current time.
        
        // You can set the hour as 5 by this method.
        date.setHours(5)

        document.write("The updated time: ")
        document.write(date.toLocaleTimeString()) // print the time after setting.
    </script>
</body>
</html

Output:

Solution 2: Using setHours() and getHours() method

The usage of the setHours() method I mentioned in solution 1. So you will walk through to learn about the getHours() method.

The getHours() method will return the hour of the date.

Syntax:

Date.getHours()

Parameters:

  • None

Return value:

The hour of the date.

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

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Add Hours To A Date Object Using JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: rebeccapurple;">
        How To Add Hours To A Date Object Using JavaScript
    </h1>
    <h2 style="color:rgb(52, 150, 206);">
        Add Hours To A Date Object Using JavaScript
    </h2>
    <h3: style="color: black;">
        Solution 2: Using setHours() and getHours() method

    </h3>
    <script>
        document.write("<br>The current time: ");
        const date = new Date();
        document.write(date.toLocaleTimeString()) // print the current time.

        // This is the function to add Hours to a Date object.
        function addHours(date,hours){
            const result = new Date(date)
            result.setHours(result.getHours()+hours)
            return result
        }

        const updated_date = addHours(date,5)
        document.write("<br>The updated time: "+updated_date.toLocaleTimeString())
    </script>
</body>
</html

Output :

Solution 3: Use the setTime() method 

The setTime() method helps you set the time or date by assigning the milliseconds value.

Syntax:

Date.setTime(value)

Parameters:

  • value: milliseconds

Return value:

The number of milliseconds from midnight January 1 1970 to now.

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

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Add Hours To A Date Object Using JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: rebeccapurple;">
        How To Add Hours To A Date Object Using JavaScript
    </h1>
    <h2 style="color:rgb(52, 150, 206);">
        Add Hours To A Date Object Using JavaScript
    </h2>
    <h3: style="color: black;">
         Solution 3: Use the setTime() method
    </h3>
    <script>
        document.write("<br>The current time: ");
        const date = new Date();

        document.write(date.toLocaleTimeString()) // print the current time.

        // This is the function to add Hours to a Date object.
        function addHours(date,hours){
            const result = new Date(date)
            result.setTime(result.getTime()+hours*60*60*1000)
            return result
        }

        const updated_date = addHours(date,5)
        document.write("<br>The updated time: "+updated_date.toLocaleTimeString())
    </script>
</body>
</html

Output:

Summary

To add hours to a date object using JavaScript, you can use the setHours() method, the setHours() method combined with the getHours() method, or the setTime() method. Choose the method that is the best for you.

Maybe you are interested:

Leave a Reply

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