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
- 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
- MilliSecond() {
- var millisecond = new Date();
- var span = document.createElement("span");
- span.innerText = "Current Time is -> " + millisecond + "\n";
- document.body.appendChild(span);
- millisecond.setMilliseconds(225);
- var span = document.createElement("span");
- span.style.color = "#00CC99";
- span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";
- document.body.appendChild(span);
- }
Complete Program
Milliseconds.ts
- class MilliSeconds
- {
- MilliSecond()
- {
- var millisecond = new Date();
- var span = document.createElement("span");
- span.innerText = "Current Time is -> " + millisecond + "\n";
- document.body.appendChild(span);
- millisecond.setMilliseconds(225);
- var span = document.createElement("span");
- span.style.color = "#00CC99";
- span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";
- document.body.appendChild(span);
- }
- }
- window.onload = () =>
- {
- var obj = new MilliSeconds();
- var bttn = < HTMLButtonElement > (document.getElementById("Button1"));
- bttn.onclick = function()
- {
- obj.MilliSecond();
- }
- };
setMilliseconds_MethodDemo.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="Milliseconds.js"></script>
- </head>
- <body>
- <h3>setMilliseconds method in TypeScript</h3>
- <div id="content" style="font-weight: normal; font-size: small">Click the button to display a date after changing the Milliseconds
- <br />
- <br />
- <input id="Button1" type="button" value="Change Milliseconds" />
- </div>
- </body>
- </html>
Milliseconds.js
- var MilliSeconds = (function() {
- function MilliSeconds() {}
- MilliSeconds.prototype.MilliSecond = function() {
- var millisecond = new Date();
- var span = document.createElement("span");
- span.innerText = "Current Time is -> " + millisecond + "\n";
- document.body.appendChild(span);
- millisecond.setMilliseconds(225);
- var span = document.createElement("span");
- span.style.color = "#00CC99";
- span.innerText = "After change Milliseconds, Time is -> " + millisecond.getMilliseconds() + "\n";
- document.body.appendChild(span);
- };
- return MilliSeconds;
- })();
- window.onload = function() {
- var obj = new MilliSeconds();
- var bttn = (document.getElementById("Button1"));
- bttn.onclick = function() {
- obj.MilliSecond();
- };
- };
- //@ sourceMappingURL=Milliseconds.js.map
Output

For more information, download the attached sample application.

Join the conversation! Your thoughts help the community grow.