Date Object - Quick JavaScript Interview Questions (2015)

Quick JavaScript Interview Questions (2015)

Chapter 6. Date Object

Q. What is a JavaScript Date Object?
JavaScript Date object returns time in that single moment. It contains the value in number of milliseconds since 1 January, 1970 UTC.

Q. What are the different ways to create a Date object? Or what are the different constructor forms of Date object?
JavaScript Date object has 4 overloaded constructors. These constructors are listed below.

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Q. How to get 1st January 1970 in a date object, the date from where time is gets calculated?
1st January 1970 is the day from where the time is get calculated as number of milliseconds elapsed. It can be calculated by creating a new JS object with parameter 0.below screenshot demonstrate the date object with parameter 0.

Q. How JavaScript counts months?
JavaScript counts months from 0 to 11 for the twelve months a year. 0 represents January and 11 represent December. Below screenshot shows the code for today’s date that is July 23 and year 2014 and getMonth() returns 6.

Q. When RangeError invalid date exception occurs?
When a date object is created with invalid date string and toISOString() method is applied to this invalid date then this RangeError occurs. Below screenshot shows '2014-13-13' string which is representing YYYY-MM-DD format where month is out of range produces the RangeError exception.