Reader Level:
ARTICLE

Date Object Method In TypeScript: Part 2

Posted by Nitin Bhardwaj Articles | TypeScript February 12, 2013
In this article you will learn about the date object's "getFullYear" and "getHours" methods in TypeScript.
  • 0
  • 0
  • 1835

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

  1.  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 date and time of the user's computer. The Date object also provides a number of functions dealing with something Coordinated Universal Time (UTC) time, also known as Greenwich Mean Time (GMT).

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

getFullYear() Method

In TypeScript,
The getFullYear() method of the date object will return the year of the specified date or the current year. The returned value is an absolute number and four-digit number.

Syntax

Date.getFullYear()

Function

getFullYear()

    {

        var year = new Date();

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

        span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";

        document.body.appendChild(span);

    }

 

 

getHours() Method

In TypeScript, the getHours() method of the date object will be used to get the hour of the specified date and time or current time. The returned value is an integer number from 0 to 23.

Syntax

Date.getHours()


F
unction

 

getHours()

    {

        var hours = new Date();

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

        span.style.color = "Blue";

        span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

getFullYear_getHours.ts

class getFullYear_getHours

{

    getFullYear()

    {

        var year = new Date();

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

        span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";

        document.body.appendChild(span);

    }

    getHours()

    {

        var hours = new Date();

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

        span.style.color = "Blue";

        span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new getFullYear_getHours();

    obj.getFullYear();

    obj.getHours();

};

 

getFullYear_getHours_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="getFullYear_getHours.js"></script>

</head>

<body>

    <h3>getFullYear and getHours method in TypeScript</h3>

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

</body>

</html>

 

getFullYear_getHours.js

var getFullYear_getHours = (function () {

    function getFullYear_getHours() { }

    getFullYear_getHours.prototype.getFullYear = function () {

        var year = new Date();

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

        span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";

        document.body.appendChild(span);

    };

    getFullYear_getHours.prototype.getHours = function () {

        var hours = new Date();

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

        span.style.color = "Blue";

        span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";

        document.body.appendChild(span);

    };

    return getFullYear_getHours;

})();

window.onload = function () {

    var obj = new getFullYear_getHours();

    obj.getFullYear();

    obj.getHours();

};

//@ sourceMappingURL=getFullYear_getHours.js.map

 

Output

 result.jpg

COMMENT USING

Trending up