Date Object Method In TypeScript: Part 8

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

 

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 "setDate" method in the TypeScript.

setDate() Method

In TypeScript, the setDate() method sets the day of the month to the date object according to the specified date or local time.

Syntax

Date.setDate(dayvalue)

dayValue is an integer from 1 to 31, representing the day of the month. If dayValue is 0 then it will result in the last hour of the previous month. And if dayValue is -1 then it will result in the hour before the last hour of the previous month. If the month has 31 days and we enter dayvalue 32 then it will result in the first day of the next month.

Function

setdate()

    {

        var date = new Date();

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

        span.innerText = "setDate Method \n Current Date is -> " + date + "\n";

        document.body.appendChild(span);

        date.setDate(12);

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

        span.style.color = "Blue";

        span.innerText = "After Change Date is -> " + date + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

setdate.ts

class setDate

{

    setdate()

    {

        var date = new Date();

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

        span.innerText = "setDate Method \n Current Date is -> " + date + "\n";

        document.body.appendChild(span);

        date.setDate(12);

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

        span.style.color = "Blue";

        span.innerText = "After Change Date is -> " + date + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new setDate();

    obj.setdate();

};

 

setDate_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="setDate.js"></script>

</head>

<body>

    <h3>setDate method in TypeScript</h3>

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

</body>

</html>

 

setDate.js

var setDate = (function ()

{

    function setDate() { }

    setDate.prototype.setdate = function ()

    {

        var date = new Date();

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

        span.innerText = "setDate Method \n Current Date is -> " + date + "\n";

        document.body.appendChild(span);

        date.setDate(12);

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

        span.style.color = "Blue";

        span.innerText = "After Change Date is -> " + date + "\n";

        document.body.appendChild(span);

    };

    return setDate;

})();

window.onload = function ()

{

    var obj = new setDate();

    obj.setdate();

};

//@ sourceMappingURL=setDate.js.map

 

Output


result.jpg

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all