Date Object Method In TypeScript: Part 9

Date Object Method In TypeScript: Part 9 

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

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

setFullYear() Method

 
In TypeScript, the setFullYear() method of the date object sets the year of the specified date according to the local time. We can also set the month and day of the month by this method.
 
Syntax
  1. Date.setFullYear(yearvalue,monthvalue,dayvalue)  
  • yearValue : It is the required parameter. A year is represented by a four-digit value. For example, in 2012.
  • monthValue : It is the optional parameter. Months are 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 month is represented by an integer between 1 and 31. If you provide the dayValue parameter then you must also provide the monthValue.
Function
  1. setfullyear() {  
  2.  var year = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "Current year is -> " + year + "\n";  
  5.  document.body.appendChild(span);  
  6.  year.setFullYear(2007);  
  7.  var span = document.createElement("span");  
  8.  span.style.color = "Blue";  
  9.  span.innerText = "After set year is -> " + year + "\n";  
  10.  document.body.appendChild(span);  
  11. }  

Complete Program

 
setFullYear.ts
  1. class setFullYear {  
  2.  setfullyear() {  
  3.   var year = new Date();  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "Current year is -> " + year + "\n";  
  6.   document.body.appendChild(span);  
  7.   year.setFullYear(2007);  
  8.   var span = document.createElement("span");  
  9.   span.style.color = "Blue";  
  10.   span.innerText = "After set year is -> " + year + "\n";  
  11.   document.body.appendChild(span);  
  12.  }  
  13. }  
  14. window.onload = () => {  
  15.  var obj = new setFullYear();  
  16.  var bttn = < HTMLButtonElement > (document.getElementById("Button1"));  
  17.  bttn.onclick = function() {  
  18.   obj.setfullyear();  
  19.  }  
  20. };  
setFullYear_MethodDemo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="setFullYear.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>setFullYear method in TypeScript</h3>  
  12.         <div id="content" style="font-weight: normal; font-size: small">Click on button for 6 year ago  
  13.             <br />  
  14.             <br />  
  15.             <input id="Button1" type="button" value="Click" />  
  16.         </div>  
  17.     </body>  
  18. </html>  
setFullYear.js
  1. var setFullYear = (function() {  
  2.  function setFullYear() {}  
  3.  setFullYear.prototype.setfullyear = function() {  
  4.   var year = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Current year is -> " + year + "\n";  
  7.   document.body.appendChild(span);  
  8.   year.setFullYear(2007);  
  9.   var span = document.createElement("span");  
  10.   span.style.color = "Blue";  
  11.   span.innerText = "After set year is -> " + year + "\n";  
  12.   document.body.appendChild(span);  
  13.  };  
  14.  return setFullYear;  
  15. })();  
  16. window.onload = function() {  
  17.  var obj = new setFullYear();  
  18.  var bttn = (document.getElementById("Button1"));  
  19.  bttn.onclick = function() {  
  20.   obj.setfullyear();  
  21.  };  
  22. };  
Output
 
 Result.jpg


Similar Articles