Date Object Method In TypeScript: Part 8

Date Object Method In TypeScript: Part 8 

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

setDate() Method

 
In TypeScript, the setDate() method sets the day of the month to the date object according to the specified date or local time.
 
Syntax
  1. Date.setDate(dayvalue)  
dayValue is an integer from 1 to 31, representing the day of the month. If dayValue is 0 then it will result in the last hour of the previous month. And if dayValue is -1 then it will result in the hour before the last hour of the previous month. If the month has 31 days and we enter dayvalue 32 then it will result in the first day of the next month.
 
Function
  1. setdate() {  
  2.  var date = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "setDate Method \n Current Date is -> " + date + "\n";  
  5.  document.body.appendChild(span);  
  6.  date.setDate(12);  
  7.  var span = document.createElement("span");  
  8.  span.style.color = "Blue";  
  9.  span.innerText = "After Change Date is -> " + date + "\n";  
  10.  document.body.appendChild(span);  
  11. }   

Complete Program

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


Similar Articles