JavaScript | date.getSeconds() method
The date.getSeconds() is an inbuilt function in JavaScript which is used to fetch the seconds from given Date object.
Syntax:
DateObj.getSeconds()
In the above syntax, DateObj is a valid Date object created using Date() conctructor from which we want to fetch seconds.
Parameter: This function does not takes any parameter. It is just used along with a Date Object from which we want to fetch seconds.
Return Values: It returns the second for the given date object. Seconds is an integer value ranging from 0 to 59.
Below program illustrate the getSeconds() method:
Example 1:
-
<script>// Here a date has been assigned// while creating Date objectvarDateObj =newDate('October 15, 1996 05:35:32');// second from above Date object is being// extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
32
Errors and Exceptions:
- Example 1: Here date of the month should must lie in between 1 to 31 because none of the month have date greater than 31 that is why it returns NaN i.e, not a number because if date for the month does not exist.
<script>// Here a date has been assigned// while creating Date objectvarDateObj =newDate('October 33, 1996 05:35:32');// second from above Date object is being// extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
NaN
- Example 2: If second is not given, it returns zero (0).
<script>// Here a date has been assigned// while creating Date objectvarDateObj =newDate('October 13, 1996 05:35');// second from above Date object is being// extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
0
- Example 3: If nothing as parameter is given to the Date() constructor, it returns current second.
<script>// Creating a Date ObjectvarDateObj =newDate();// second from above Date object is being// extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
8
- Example 4: If second is 88, it return 0 as exception because seconds range is in between 0 to 59 and 88 is out of this range.
<script>// Here a date has been assigned// while creating Date objectvarDateObj =newDate('October 13, 1996 05:35:88');// second from above Date object is being// extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
0
Application: It has many applications such as getting current Second. Below program shows one of the applications of this function. It gives the current Second.
- Example 1:
<script>// Creating Date ObjectvarDateObj =newDate();// second from above Date Obect is// being extracted using getSeconds()varsec = DateObj.getSeconds();// Printing seconddocument.write(sec);</script>chevron_rightfilter_noneOutput:
8
Supported Browsers: The browsers supported by JavaScript date.getSeconds() method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- JavaScript | Array.from() method
- JavaScript | Replace() Method
- JavaScript | Array map() Method
- JavaScript | getTime() Method
- JavaScript | Array from() Method
- JavaScript | compile() Method
- JavaScript | Sort() method
- JavaScript | exec() Method
- JavaScript | Array.find() Method
- JavaScript | Array.findIndex() Method
- JavaScript | Array.splice() Method
- JavaScript | Array.fill() Method
- JavaScript | String includes() Method
- JavaScript | date.getDay() method
- JavaScript | Array reduce() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



