Date Object Method In TypeScript: Part 10

Before reading this article, please go through the following articles:

  1. Date Object Method In TypeScript: Part 1

  2. Date Object Method In TypeScript: Part 2

  3. Date Object Method In TypeScript: Part 3

  4. Date Object Method In TypeScript: Part 4

  5. Date Object Method In TypeScript: Part 5

  6. Date Object Method In TypeScript: Part 6

  7. Date Object Method In TypeScript: Part 7

  8. Date Object Method In TypeScript: Part 8

  9. Date Object Method In TypeScript: part 9

 

Introduction

The Date object is the key to date and time functionality in TypeScript. If we create it with no argument passed to its constructor, it will contain the date and time of the user's computer. The Date object also provides a number of functions dealing with something called Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT) during winter. The World Time Standard is set by the UTC time.

In this article I am describing the date object's "setHours" method in the TypeScript.

setHours() Method

In TypeScript, the setHours() method of the date object sets the hour of the local time. We can also set the minutes, seconds and milliseconds by this method.

Syntax

Date.setHours(hour,min,sec,millisec)

  • Hour It is the required parameter. Hours are represented by an integer value between 0 and 23. And -1 will result in the last hour of the previous day. And 24 will result in the first hours of the next day.

  • Min It is an optional parameter. Minutes are represented by an integer value from 0 to 59.

  • Sec It is an optional parameter. Seconds are represented by an integer value from 0 to 59.

  • Millisec It is an optional parameter. Milliseconds are represented by an integer value from 0 to 999.

Function

sethours()

    {

        var hours = new Date();

        var span = document.createElement("span");

        span.innerText = "Current Time is -> " + hours + "\n";

        document.body.appendChild(span);

        hours.setHours(2);

        var span = document.createElement("span");

        span.style.color = "Blue";

        span.innerText = "After change hours, Time is -> " + hours + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

setHours.ts

class SetHours

{

    sethours()

    {

        var hours = new Date();

        var span = document.createElement("span");

        span.innerText = "Current Time is -> " + hours + "\n";

        document.body.appendChild(span);

        hours.setHours(2);

        var span = document.createElement("span");

        span.style.color = "Blue";

        span.innerText = "After change hours, Time is -> " + hours + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new SetHours();

    var bttn = <HTMLButtonElement>(document.getElementById("Button1"));

    bttn.onclick = function ()

    {

        obj.sethours();

    }

};

 

setHours_MethodDemo.htm

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="utf-8" />

    <title>TypeScript HTML App</title>

    <link rel="stylesheet" href="app.css" type="text/css" />

    <script src="SetHours.js"></script>

</head>

<body>

    <h3>setHours method in TypeScript</h3>

    <div id="content" style="font-weight: normal; font-size: small">Click the button to display a date after changing the hours<br />

        <br />

        <input id="Button1" type="button" value="Change Hours" /></div>

</body>

</html>

 

setHours.js

var SetHours = (function () {

    function SetHours() { }

    SetHours.prototype.sethours = function () {

        var hours = new Date();

        var span = document.createElement("span");

        span.innerText = "Current Time is -> " + hours + "\n";

        document.body.appendChild(span);

        hours.setHours(2);

        var span = document.createElement("span");

        span.style.color = "Blue";

        span.innerText = "After change hours, Time is -> " + hours + "\n";

        document.body.appendChild(span);

    };

    return SetHours;

})();

window.onload = function () {

    var obj = new SetHours();

    var bttn = (document.getElementById("Button1"));

    bttn.onclick = function () {

        obj.sethours();

    };

};

//@ sourceMappingURL=SetHours.js.map

 

Output


 Result.jpg

For more information, download the attached sample application.

Up Next
    Ebook Download
    View all
    Test
    Read by 16 people
    Download Now!
    Learn
    View all