Date Object Method In TypeScript: Part 2

Date Object Method In TypeScript: Part 2 

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

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
  1. Date.getFullYear()   
Function
  1. getFullYear() {  
  2.  var year = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

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
  1. Date.getHours()  
Function
  1. getHours() {  
  2.  var hours = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.style.color = "Blue";  
  5.  span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

Complete Program

 
getFullYear_getHours.ts
  1. class getFullYear_getHours {  
  2.  getFullYear() {  
  3.   var year = new Date();  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";  
  6.   document.body.appendChild(span);  
  7.  }  
  8.  getHours() {  
  9.   var hours = new Date();  
  10.   var span = document.createElement("span");  
  11.   span.style.color = "Blue";  
  12.   span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";  
  13.   document.body.appendChild(span);  
  14.  }  
  15. }  
  16. window.onload = () => {  
  17.  var obj = new getFullYear_getHours();  
  18.  obj.getFullYear();  
  19.  obj.getHours();  
  20. };  
getFullYear_getHours_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="getFullYear_getHours.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>getFullYear and getHours method in TypeScript</h3>  
  12.         <div id="content"></div>  
  13.     </body>  
  14. </html>  
getFullYear_getHours.js
  1. var getFullYear_getHours = (function() {  
  2.  function getFullYear_getHours() {}  
  3.  getFullYear_getHours.prototype.getFullYear = function() {  
  4.   var year = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "getFullYear Method \n Current year is-> " + year.getFullYear() + "\n";  
  7.   document.body.appendChild(span);  
  8.  };  
  9.  getFullYear_getHours.prototype.getHours = function() {  
  10.   var hours = new Date();  
  11.   var span = document.createElement("span");  
  12.   span.style.color = "Blue";  
  13.   span.innerText = "getHours Method \n Current Hours is-> " + hours.getHours() + "\n";  
  14.   document.body.appendChild(span);  
  15.  };  
  16.  return getFullYear_getHours;  
  17. })();  
  18. window.onload = function() {  
  19.  var obj = new getFullYear_getHours();  
  20.  obj.getFullYear();  
  21.  obj.getHours();  
  22. };  
Output
 
 result.jpg