Date Object Method In TypeScript: Part 3

Date Object Method In TypeScript: Part 3 

 
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) time, also known (in the Winter) as Greenwich Mean Time (GMT).
 
In this article, I am describing the date object's "getMonth" and "getSeconds" method in the TypeScript.
 

getMonth() Method

 
In TypeScript, the getMonth() method will return the month number from the date. The months are numbered starting with zero. January is 0 and December is 11.
 
Syntax
  1. Date.getMonth()  
Function
  1. getMonth() {  
  2.  var CurrntMonth: string[] = ["January""February""March""April""May""June""July""August""September""October""November""December", ];  
  3.  var month = new Date();  
  4.  var CrrntMonth = CurrntMonth[month.getMonth()];  
  5.  var span = document.createElement("span");  
  6.  span.style.color = "Blue";  
  7.  span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";  
  8.  document.body.appendChild(span);  
  9. }  

getSeconds() Method

 
In TypeScript, the getSeconds() method returns the seconds of the specified date and time or the current date and time. The returned value is an integer number between 0 and 59.
 
Syntax
  1. Date.getSeconds()  
F
unction
  1. getSecond() {  
  2.  var second = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

Complete Program

 
getMonth_getSeconds.ts
  1. class getMonth_getSeconds {  
  2.  getMonth() {  
  3.   var CurrntMonth: string[] = ["January""February""March""April""May""June""July""August""September""October""November""December", ];  
  4.   var month = new Date();  
  5.   var CrrntMonth = CurrntMonth[month.getMonth()];  
  6.   var span = document.createElement("span");  
  7.   span.style.color = "Blue";  
  8.   span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";  
  9.   document.body.appendChild(span);  
  10.  }  
  11.  getSecond() {  
  12.   var second = new Date();  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var obj = new getMonth_getSeconds();  
  20.  obj.getMonth();  
  21.  obj.getSecond();  
  22. };  
getMonth_getSeconds_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="getMonth_getSeconds.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>getMonth and getSeconds method in TypeScript</h3>  
  12.         <div id="content"></div>  
  13.     </body>  
  14. </html>  
getMonth_getSeconds.js
  1. var getMonth_getSeconds = (function() {  
  2.  function getMonth_getSeconds() {}  
  3.  getMonth_getSeconds.prototype.getMonth = function() {  
  4.   var CurrntMonth = ["January""February""March""April""May""June""July""August""September""October""November""December", ];  
  5.   var month = new Date();  
  6.   var CrrntMonth = CurrntMonth[month.getMonth()];  
  7.   var span = document.createElement("span");  
  8.   span.style.color = "Blue";  
  9.   span.innerText = "getMonth Method \n Current Month is-> " + CrrntMonth + "\n";  
  10.   document.body.appendChild(span);  
  11.  };  
  12.  getMonth_getSeconds.prototype.getSecond = function() {  
  13.   var second = new Date();  
  14.   var span = document.createElement("span");  
  15.   span.innerText = "getSeconds Method \n The Seconds of the time right now-> " + second.getSeconds() + "\n";  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return getMonth_getSeconds;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new getMonth_getSeconds();  
  22.  obj.getMonth();  
  23.  obj.getSecond();  
  24. };  
Output
 
result.jpg


Similar Articles