Javascript - Date getSeconds() Method

The getSeconds() method returns the seconds ranged from 0 to 59 from the date object.

Description

The getSeconds() method returns the seconds ranged from 0 to 59 from the date object.

Syntax

Date.getSeconds()

Parameters

None

Return

A Number, from 0 to 59, representing the seconds

Example

Return the seconds, according to local time:

Demo

//display the seconds of the time right now.

var d = new Date();
var n = d.getSeconds();
console.log(n);// ww  w . ja v a2  s  .  c o  m

Result

Using getHours(), getMinutes(), and getSeconds() to display the time:

Demo

//display the time.
function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }/*from  w w w  .j  a v a2s.  c om*/
    return i;
}

var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
console.log( h + ":" + m + ":" + s);

Result