Date Object Method In TypeScript: Part 11

Date Object Method In TypeScript: Part 11 

 
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 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), 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 "setMilliseconds" method in TypeScript.
 

setMilliseconds() Method

 
In TypeScript, the setMilliseconds() method of the date object sets the milliseconds of the local time.
 
Syntax
  1. Date.setMilliseconds(millisec)  
  • Millisec It is an optional parameter. Milliseconds are represented by an integer value between 0 and 999. If we enter -1, It will return as the result the last millisecond of the previous second. And 1000 will return the result as the first millisecond of the next second.
Function
  1. MilliSecond() {  
  2.  var millisecond = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "Current Time is -> " + millisecond + "\n";  
  5.  document.body.appendChild(span);  
  6.  millisecond.setMilliseconds(225);  
  7.  var span = document.createElement("span");  
  8.  span.style.color = "#00CC99";  
  9.  span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";  
  10.  document.body.appendChild(span);  
  11. }  

Complete Program

 
Milliseconds.ts
  1. class MilliSeconds  
  2. {  
  3.  MilliSecond()  
  4.  {  
  5.   var millisecond = new Date();  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "Current Time is -> " + millisecond + "\n";  
  8.   document.body.appendChild(span);  
  9.   millisecond.setMilliseconds(225);  
  10.   var span = document.createElement("span");  
  11.   span.style.color = "#00CC99";  
  12.   span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";  
  13.   document.body.appendChild(span);  
  14.  }  
  15. }  
  16. window.onload = () =>  
  17.  {  
  18.   var obj = new MilliSeconds();  
  19.   var bttn = < HTMLButtonElement > (document.getElementById("Button1"));  
  20.   bttn.onclick = function()  
  21.   { 
  22.    obj.MilliSecond();  
  23.   }  
  24.  };  
setMilliseconds_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="Milliseconds.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>setMilliseconds method in TypeScript</h3>  
  12.         <div id="content" style="font-weight: normal; font-size: small">Click the button to display a date after changing the Milliseconds  
  13.             <br />  
  14.             <br />  
  15.             <input id="Button1" type="button" value="Change Milliseconds" />  
  16.         </div>  
  17.     </body>  
  18. </html>  
Milliseconds.js
  1. var MilliSeconds = (function() {  
  2.  function MilliSeconds() {}  
  3.  MilliSeconds.prototype.MilliSecond = function() {  
  4.   var millisecond = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Current Time is -> " + millisecond + "\n";  
  7.   document.body.appendChild(span);  
  8.   millisecond.setMilliseconds(225);  
  9.   var span = document.createElement("span");  
  10.   span.style.color = "#00CC99";  
  11.   span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";  
  12.   document.body.appendChild(span);  
  13.  };  
  14.  return MilliSeconds;  
  15. })();  
  16. window.onload = function() {  
  17.  var obj = new MilliSeconds();  
  18.  var bttn = (document.getElementById("Button1"));  
  19.  bttn.onclick = function() {  
  20.   obj.MilliSecond();  
  21.  };  
  22. };  
  23. //@ sourceMappingURL=Milliseconds.js.map  
Output
 
result.jpg
 
For more information, download the attached sample application.


Similar Articles