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 setTime 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
- 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
settime()
{
var time = new Date();
var span = document.createElement("span");
span.innerHTML = "Current Date is -> " + time + "\n";
document.body.appendChild(span);
time.setTime(1332403882588);
var span = document.createElement("span");
span.style.color = "green";
span.innerHTML = "Date after setting the time -> " + time + "\n";
document.body.appendChild(span);
}
Complete Program
setTime.ts
class setTime
{
settime()
{
var time = new Date();
var span = document.createElement("span");
span.innerHTML = "Current Date is -> " + time + "\n";
document.body.appendChild(span);
time.setTime(1332403882588);
var span = document.createElement("span");
span.style.color = "green";
span.innerHTML = "Date after setting the time -> " + time + "\n";
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new setTime();
var bttn = <HTMLButtonElement>(document.getElementById("Button1"));
bttn.onclick = function ()
{
obj.settime();
}
};
default.htm
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="setTime.js"></script>
</head>
<body>
<h3>setTime method in TypeScript</h3>
<div id="content" style="font-weight: normal; font-size: small">Click on button for display the date after setting the time<br />
<br />
<input id="Button1" type="button" value="Click" /></div>
</body>
</html>
setTime.js
var setTime = (function () {
function setTime() { }
setTime.prototype.settime = function () {
var time = new Date();
var span = document.createElement("span");
span.innerHTML = "Current Date is -> " + time + "\n";
document.body.appendChild(span);
time.setTime(1332403882588);
var span = document.createElement("span");
span.style.color = "green";
span.innerHTML = "Date after setting the time -> " + time + "\n";
document.body.appendChild(span);
};
return setTime;
})();
window.onload = function () {
var obj = new setTime();
var bttn = (document.getElementById("Button1"));
bttn.onclick = function () {
obj.settime();
};
};
//@ sourceMappingURL=setTime.js.map
Output
For more information, download attached sample application.