Date Object Method In TypeScript: Part 1

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 current 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 as Greenwich Mean Time (GMT).

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

getDate() Method

In TypeScript,
The getDate() method will return an integer between 1 and 31 that represents the day-of-the-month.

Syntax

Date.getDate()

Function

getDate()

    {

        var date = new Date();

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

        span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";

        document.body.appendChild(span);

    }

 

 

getDay() Method

In TypeScript, the getDay() method of the date object gets the day of the week between 0 and 6 for the specified date.

Syntax

Date.getDay()


F
unction

 

getDay()

    {

        var Wday: string[] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

        var day = new Date();

        var TodayDay = Wday[day.getDay()];   

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

        span.style.color = "Blue";

        span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

getDate_getDay.ts

class getDate_getDay

{

    getDate()

    {

        var date = new Date();

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

        span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";

        document.body.appendChild(span);

    }

    getDay()

    {

        var Wday: string[] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

        var day = new Date();

        var TodayDay = Wday[day.getDay()];   

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

        span.style.color = "Blue";

        span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new getDate_getDay();

    obj.getDate();

    obj.getDay();

};

 

getDate_getDay_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="getDate_getDay.js"></script>

</head>

<body>

    <h3>getDate and getDay method in TypeScript</h3>

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

</body>

</html>

 

getDate_getDay.js

var getDate_getDay = (function () {

    function getDate_getDay() { }

    getDate_getDay.prototype.getDate = function () {

        var date = new Date();

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

        span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";

        document.body.appendChild(span);

    };

    getDate_getDay.prototype.getDay = function () {

        var Wday = [

            "Sunday",

            "Monday",

            "Tuesday",

            "Wednesday",

            "Thursday",

            "Friday",

            "Saturday"

        ];

        var day = new Date();

        var TodayDay = Wday[day.getDay()];

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

        span.style.color = "Blue";

        span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";

        document.body.appendChild(span);

    };

    return getDate_getDay;

})();

window.onload = function () {

    var obj = new getDate_getDay();

    obj.getDate();

    obj.getDay();

};

//@ sourceMappingURL=getDate_getDay.js.map

 

Output

 result.jpg

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