Date Object Method In TypeScript: Part 3

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

 

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) time, also known (in the Winter) as Greenwich Mean Time (GMT).

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

getMonth() Method

In TypeScript, the getMonth() method will return the month number from the date. The months are numbered starting with zero. January is 0 and December is 11.

Syntax

Date.getMonth()

Function

getMonth()

    {

        var CurrntMonth: string[] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ];

        var month = new Date();

        var CrrntMonth = CurrntMonth[month.getMonth()];

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

        span.style.color = "Blue";

        span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";

        document.body.appendChild(span);    

    }

 

 

getSeconds() Method

In TypeScript, the getSeconds() method returns the seconds of the specified date and time or the current date and time. The returned value is an integer number between 0 and 59.

Syntax

Date.getSeconds()


F
unction

 

getSecond()

    {

        var second = new Date();

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

        span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

getMonth_getSeconds.ts

class getMonth_getSeconds

{

    getMonth()

    {

        var CurrntMonth: string[] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ];

        var month = new Date();

        var CrrntMonth = CurrntMonth[month.getMonth()];

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

        span.style.color = "Blue";

        span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";

        document.body.appendChild(span);    

    }

    getSecond()

    {

        var second = new Date();

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

        span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new getMonth_getSeconds();

    obj.getMonth();

    obj.getSecond();

};

 

getMonth_getSeconds_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="getMonth_getSeconds.js"></script>

</head>

<body>

    <h3>getMonth and getSeconds method in TypeScript</h3>

    <div id="content"></div>

</body>

</html>

 

getMonth_getSeconds.js

var getMonth_getSeconds = (function () {

    function getMonth_getSeconds() { }

    getMonth_getSeconds.prototype.getMonth = function () {

        var CurrntMonth = [

            "January",

            "February",

            "March",

            "April",

            "May",

            "June",

            "July",

            "August",

            "September",

            "October",

            "November",

            "December",

           

        ];

        var month = new Date();

        var CrrntMonth = CurrntMonth[month.getMonth()];

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

        span.style.color = "Blue";

        span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";

        document.body.appendChild(span);

    };

    getMonth_getSeconds.prototype.getSecond = function () {

        var second = new Date();

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

        span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";

        document.body.appendChild(span);

    };

    return getMonth_getSeconds;

})();

window.onload = function () {

    var obj = new getMonth_getSeconds();

    obj.getMonth();

    obj.getSecond();

};

//@ sourceMappingURL=getMonth_getSeconds.js.map

 

Output


result.jpg

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