Date Object Method In TypeScript: Part 1

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

getDate() Method

 
In TypeScript, The getDate() method will return an integer between 1 and 31 that represents the day-of-the-month.
 
Syntax
  1. Date.getDate()  
Function
  1. getDate() {  
  2.  var date = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

getDay() Method

 
In TypeScript, the getDay() method of the date object gets the day of the week between 0 and 6 for the specified date.
 
Syntax
  1. Date.getDay()  
Function
  1. getDay() {  
  2.  var Wday: string[] = ["Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"];  
  3.  var day = new Date();  
  4.  var TodayDay = Wday[day.getDay()];  
  5.  var span = document.createElement("span");  
  6.  span.style.color = "Blue";  
  7.  span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";  
  8.  document.body.appendChild(span);  
  9. }  

Complete Program

 
getDate_getDay.ts
  1. class getDate_getDay  
  2. {  
  3.  getDate()  
  4.  {  
  5.   var date = new Date();  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";  
  8.   document.body.appendChild(span);  
  9.  }  
  10.  getDay()  
  11.  {  
  12.   var Wday: string[] = ["Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"];  
  13.   var day = new Date();  
  14.   var TodayDay = Wday[day.getDay()];  
  15.   var span = document.createElement("span");  
  16.   span.style.color = "Blue";  
  17.   span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";  
  18.   document.body.appendChild(span);  
  19.  }  
  20. }  
  21. window.onload = () =>  
  22.  {  
  23.   var obj = new getDate_getDay();  
  24.   obj.getDate();  
  25.   obj.getDay();  
  26.  };  
getDate_getDay_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="getDate_getDay.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>getDate and getDay method in TypeScript</h3>  
  12.         <div id="content"></div>  
  13.     </body>  
  14. </html>  
getDate_getDay.js
  1. var getDate_getDay = (function() {  
  2.  function getDate_getDay() {}  
  3.  getDate_getDay.prototype.getDate = function() {  
  4.   var date = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "getDate Method \n Today Date is-> " + date.getDate() + "\n";  
  7.   document.body.appendChild(span);  
  8.  };  
  9.  getDate_getDay.prototype.getDay = function() {  
  10.   var Wday = ["Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"];  
  11.   var day = new Date();  
  12.   var TodayDay = Wday[day.getDay()];  
  13.   var span = document.createElement("span");  
  14.   span.style.color = "Blue";  
  15.   span.innerText = "getDay Method \n Today Day is-> " + TodayDay + "\n";  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return getDate_getDay;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new getDate_getDay();  
  22.  obj.getDate();  
  23.  obj.getDay();  
  24. };  
Output
 
 result.jpg


Similar Articles