JavaScript Date.setMilliseconds()

Why setMilliseconds ?

The JavaScript Date setMilliseconds () method is a built-in function. sets milliseconds for a specific date by local time.

Syntax:

Date.setMilliseconds(millisecondsValue)

  • millisecondsValue :Represents milliseconds. Gets values ​​from 0 to 999.
  • If you specify a number outside the expected range, the date information in the Date object is updated accordingly. For example, if you specify 1010, the number of seconds is incremented by 1, and 10 is used for the milliseconds.

JavaScript setMilliseconds Examples

Example 1

The setMilliseconds () method can change the millisecond value of a given date.

        var date = new Date('April 21, 1983 22:47:55:132');
        console.log(date.getMilliseconds());

        date.setMilliseconds(554);
        console.log(date.getMilliseconds())

132
554


Example 2

The setMilliseconds () method can change the millisecond value of the current time.

        var now = new Date();
        console.log(now.getMilliseconds());

        date.setMilliseconds(254);
        console.log(date.getMilliseconds())

887
254


Example 3

  • If you specify a number outside the expected range, the date information in the Date object is updated accordingly. For example, if you specify 1010, the number of seconds is incremented by 1, and 10 is used for the milliseconds.
        var date = new Date('April 21, 1983 13:22:32:77');
        console.log(date.getMilliseconds());

        date.setMilliseconds(2004);
        console.log(date.getMilliseconds())

77
4


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started