Date

Date.prototype.getSeconds

Returns the seconds (0-59) in the specified date according to local time

Syntax

JavaScript
date.getSeconds()

Return Value

An integer from 0 to 59

Examples

Basic Usage
const date = new Date('2024-06-15T14:30:45');
console.log(date.getSeconds()); // 45
Practical Example
const now = new Date();
const timestamp = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
console.log(timestamp);
Advanced Usage
function formatTime(date: Date) {
  const pad = (n: number) => String(n).padStart(2, '0');
  return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
}

Understanding Date.prototype.getSeconds

The Date.prototype.getSeconds method in JavaScript returns the seconds (0-59) in the specified date according to local time. It belongs to the Date object and is one of the most widely used methods for working with date values in modern JavaScript and TypeScript applications.

The method signature is date.getSeconds(). When called, it returns an integer from 0 to 59. Understanding when and how to use getSeconds() helps you write more expressive, readable code.

Common use cases for Date.prototype.getSeconds include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like date-getminutes, date-gethours, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Date.prototype.getSeconds is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Date Methods

Other methods in the Date object

Related Tools

More Date Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.