Computer >> Computer tutorials >  >> Programming >> Javascript

Date.getUTCSeconds() function in JavaScript


The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The getUTCSeconds() function of the Date object returns the seconds in the given date according to the universal time.

Syntax

Its Syntax is as follows

dateObj.getUTCSeconds();

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4:25:96');
      document.write(dateObj.getUTCSeconds());
   </script>
</body>
</html>

Output

25

Example

Incase if you haven’t mentioned the second’s value while creating the date object this function returns 0 according to the universal time.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4');
      document.write("Seconds: "+dateObj.getSeconds());
   </script>
</body>
</html>

Output

Seconds: 0

Example

In the same way if you haven’t passed anything while creating the date object this function returns the Current seconds according to the universal time.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      document.write("Seconds: "+dateObj.getUTCSeconds());
   </script>
</body>
</html>

Output

Seconds: 49

Example

The range of the seconds in JavaScript is from 0 to 59 if you use value out of this range this function returns 0 according to the universal time.

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 04:50:100');
      document.write("Current milliseconds: "+dateObj.getUTCSeconds());
   </script>
</body>
</html>

Output

Current milliseconds: 0