How To Set Month In TypeScript

Introduction

The Date object is the key to date and time functionality in TypeScript. If we create it with no argument passed in 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), 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 how to set the month by you in TypeScript.

setMonths() Method

In TypeScript, the setMonth() method sets the month of the specified date or the current date depending on the local time. We can also set the day of the month by this method.

Syntax

Date.setMonth(monthvalue,dayvalue)

  • monthValue: It is the required parameter. The month is represented by an integer value between 0 and 11. 0 is January, 1 is February and so on.
  • dayValue: It is an optional parameter. A day of the month is represented by an integer between 1 and 31.

Function

setmonth()

    {

        var month = new Date();

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

        span.innerText = "Current Date is -> " + month + "\n";

        document.body.appendChild(span);

        month.setMonth(2);

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

        span.style.color = "green";

        span.innerText = "After set month Date is -> " + month + "\n";

        document.body.appendChild(span);

    }

 

Complete Program

setMonth.ts

class setMonth

{

    setmonth()

    {

        var month = new Date();

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

        span.innerText = "Current Date is -> " + month + "\n";

        document.body.appendChild(span);

        month.setMonth(2);

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

        span.style.color = "green";

        span.innerText = "After set month Date is -> " + month + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new setMonth();

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

    bttn.onclick = function () {

        obj.setmonth();

    }

};

 

setMonth_Demo.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="setMonth.js"></script>

</head>

<body>

    <h3>setMonth method in TypeScript</h3>

    <div id="content" style="font-weight: normal; font-size: small">Click on button for set 2 month forth<br />

        <br />

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

</body>

</html>

 

setMonth.js

var setMonth = (function ()

{

    function setMonth() {

    }

    setMonth.prototype.setmonth = function ()

    {

        var month = new Date();

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

        span.innerText = "Current Date is -> " + month + "\n";

        document.body.appendChild(span);

        month.setMonth(2);

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

        span.style.color = "green";

        span.innerText = "After set month Date is -> " + month + "\n";

        document.body.appendChild(span);

    };

    return setMonth;

})();

window.onload = function ()

{

    var obj = new setMonth();

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

    bttn.onclick = function ()

    {

        obj.setmonth();

    };

};

//@ sourceMappingURL=setMonth.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