JavaScript date.getUTCSeconds() method
The date.getUTCSeconds()
method in JavaScript returns the seconds (from 0 to 59) of a specified Date
object according to Universal Coordinated Time (UTC). This method is useful for obtaining the seconds of a date without considering local time zone differences.
Syntax:
Returns:
- An integer representing the seconds of the minute (from 0 to 59).
Example 1: Getting UTC Seconds for a Specific Date
Output:
Explanation:
- The
Date
object represents October 22, 2024, at 15:30:45 UTC. ThegetUTCSeconds()
method returns45
, which corresponds to the seconds in the given time.
Example 2: Getting UTC Seconds for a Local Time
Output:
Explanation:
- If the local time is 12:00:30 PM, this will convert to a different UTC time depending on the local timezone. If your local timezone is UTC-3, then it would convert to 3:00:30 PM UTC. The
getUTCSeconds()
method returns30
, which corresponds to the seconds in that UTC time.
Example 3: Getting UTC Seconds for a Date Near Midnight
Output:
Explanation:
- If your local timezone is UTC-3, then the local time of 23:59:59 on October 31 will convert to 02:59:59 UTC on November 1. Therefore,
getUTCSeconds()
returns59
, representing the last second before midnight in UTC.
Example 4: Getting UTC Seconds for a Date Object Created with UTC
Output:
Explanation:
- The
Date.UTC()
method creates a date object for October 22, 2024, at 15:30:10 in UTC. ThegetUTCSeconds()
method returns10
, confirming the correct seconds in the UTC time.
Summary:
date.getUTCSeconds()
returns the seconds of aDate
object in UTC, ranging from 0 to 59.- This method is helpful for working with time in a consistent manner, independent of local timezone adjustments.