How To Use SetTime Method in TypeScript

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 how to use the time method of date object in TypeScript.
  

setTime() Method

 
In TypeScript, the setTime() method of date object sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970.
 
Syntax
  1. Date.setTime(millisec)  
  • millisec : It is the required parameter. In the setTime method, the number of milliseconds to be added to, or subtracted from, midnight January 1, 1970
Function
  1. settime() {  
  2.  var time = new Date();  
  3.  var span = document.createElement("span");  
  4.  span.innerHTML = "Current Date is -> " + time + "\n";  
  5.  document.body.appendChild(span);  
  6.  time.setTime(1332403882588);  
  7.  var span = document.createElement("span");  
  8.  span.style.color = "green";  
  9.  span.innerHTML = "Date after setting the time -> " + time + "\n";  
  10.  document.body.appendChild(span);  
  11. }  

Complete Program

 
setTime.ts
  1. class setTime {  
  2.  settime() {  
  3.   var time = new Date();  
  4.   var span = document.createElement("span");  
  5.   span.innerHTML = "Current Date is -> " + time + "\n";  
  6.   document.body.appendChild(span);  
  7.   time.setTime(1332403882588);  
  8.   var span = document.createElement("span");  
  9.   span.style.color = "green";  
  10.   span.innerHTML = "Date after setting the time -> " + time + "\n";  
  11.   document.body.appendChild(span);  
  12.  }  
  13. }  
  14. window.onload = () => {  
  15.  var obj = new setTime();  
  16.  var bttn = < HTMLButtonElement > (document.getElementById("Button1"));  
  17.  bttn.onclick = function() {  
  18.   obj.settime();  
  19.  }  
  20. };  
default.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "setTime.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 > setTime method in TypeScript < /h3>  
  23.  <  
  24.  div id = "content"  
  25. style = "font-weight: normal; font-size: small" > Click on button  
  26. for display the date after setting the time < br / >  
  27.  <  
  28.  br / >  
  29.  <  
  30.  input id = "Button1"  
  31. type = "button"  
  32. value = "Click" / > < /div>  
  33.  <  
  34.  /body>  
  35.  <  
  36.  /html>  
setTime.js
  1. var setTime = (function() {  
  2.  function setTime() {}  
  3.  setTime.prototype.settime = function() {  
  4.   var time = new Date();  
  5.   var span = document.createElement("span");  
  6.   span.innerHTML = "Current Date is -> " + time + "\n";  
  7.   document.body.appendChild(span);  
  8.   time.setTime(1332403882588);  
  9.   var span = document.createElement("span");  
  10.   span.style.color = "green";  
  11.   span.innerHTML = "Date after setting the time -> " + time + "\n";  
  12.   document.body.appendChild(span);  
  13.  };  
  14.  return setTime;  
  15. })();  
  16. window.onload = function() {  
  17.  var obj = new setTime();  
  18.  var bttn = (document.getElementById("Button1"));  
  19.  bttn.onclick = function() {  
  20.   obj.settime();  
  21.  };  
  22. };  
Output
 
 result.jpg
 
For more information, download the attached sample application.
 


Similar Articles