Skip to content
Advertisements

JavaScript Date.setSeconds()

Why setSeconds ?

The JavaScript Date setSeconds () method is a built-in function. The function of this function; The default is to set the date or the seconds on the entered date. (can be adjusted in milliseconds)

Syntax:

DateObj.setSeconds(seconds)

  • seconds: Seconds. It takes a value between 0-59.
  • 1 will result in the last second of the previous minute
  • 60 will result in the first second of the next minute
  • If we are adding milliseconds, we should use:

DateObj.setSeconds(seconds, miliseconds)

  • milliseconds . It takes a value between 0-999.
  • -1 will result in the last millisecond of the previous second
  • 1000 will result in the first millisecond of the next second

Return:

  • The number of milliseconds between 1 January 1970 00:00:00 UTC and the updated date.

JavaScript setSeconds Examples

Example 1

You can change the seconds value of a specific date.

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

        date.setSeconds(18);
        console.log(date.getSeconds())

55
18


Example 2

You can change the seconds value of your current time.

        var date = new Date();
        console.log(date.getSeconds());

        date.setSeconds(47);
        console.log(date.getSeconds())

11
47


Example 3

        var date = new Date('April 21, 1983 12:47:25');
        console.log(date.getSeconds());

        date.setSeconds(74); // 74 - 60 = 14
        console.log(date.getSeconds())

25
14


Example 4

        var date = new Date('October 22, 2019 15:22:17:432');
        console.log("sec: " + date.getSeconds());
        console.log("milisec: " + date.getMilliseconds());

        date.setSeconds(25,15);
        console.log("sec: " + date.getSeconds());
        console.log("milisec: " + date.getMilliseconds());

sec: 17
milisec: 432
sec: 25
milisec: 15


Browser Support

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: