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
  1. Date.setMonth(monthvalue,dayvalue)  
  • monthValue: It is the required parameter. The month is represented by an integer value between 0 and 11. 0 on January, 1 in 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
  1. setmonth() {  
  2.  var month = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "Current Date is -> " + month + "\n";  
  5.  document.body.appendChild(span);  
  6.  month.setMonth(2);  
  7.  var span = document.createElement("span");  
  8.  span.style.color = "green";  
  9.  span.innerText = "After set month Date is -> " + month + "\n";  
  10.  document.body.appendChild(span);  
  11. }  

Complete Program

 
setMonth.ts
  1. class setMonth {  
  2.  setmonth() {  
  3.   var month = new Date();  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "Current Date is -> " + month + "\n";  
  6.   document.body.appendChild(span);  
  7.   month.setMonth(2);  
  8.   var span = document.createElement("span");  
  9.   span.style.color = "green";  
  10.   span.innerText = "After set month Date is -> " + month + "\n";  
  11.   document.body.appendChild(span);  
  12.  }  
  13. }  
  14. window.onload = () => {  
  15.  var obj = new setMonth();  
  16.  var bttn = < HTMLButtonElement > (document.getElementById("Button1"));  
  17.  bttn.onclick = function() {  
  18.   obj.setmonth();  
  19.  }  
  20. };  
setMonth_Demo.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "setMonth.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 > setMonth method in TypeScript < /h3>  
  23.  <  
  24.  div id = "content"  
  25. style = "font-weight: normal; font-size: small" > Click on button  
  26. for set 2 month forth < br / >  
  27.  <  
  28.  br / >  
  29.  <  
  30.  input id = "Button1"  
  31. type = "button"  
  32. value = "Click" / > < /div>  
  33.  <  
  34.  /body>  
  35.  <  
  36.  /html>  
setMonth.js
  1. var setMonth = (function() {  
  2.  function setMonth() {}  
  3.  setMonth.prototype.setmonth = function() {  
  4.   var month = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Current Date is -> " + month + "\n";  
  7.   document.body.appendChild(span);  
  8.   month.setMonth(2);  
  9.   var span = document.createElement("span");  
  10.   span.style.color = "green";  
  11.   span.innerText = "After set month Date is -> " + month + "\n";  
  12.   document.body.appendChild(span);  
  13.  };  
  14.  return setMonth;  
  15. })();  
  16. window.onload = function() {  
  17.  var obj = new setMonth();  
  18.  var bttn = (document.getElementById("Button1"));  
  19.  bttn.onclick = function() {  
  20.   obj.setmonth();  
  21.  };  
  22. };   
Output
 
result.jpg
 
For more information, download the attached sample application.


Similar Articles