JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – zero date can give get full year method

JavaScript Quiz – zero date can give get full year method

Date Get Full Year Method

Javascript date is an object which contains full detail of date-time. Javascript date has a method get Full Year method which provides only the year of date in date object.

Need to know javascript date format like dd-mm-yyyy or yyyy-MM-dd

It’s syntax be like –

let date = new Date();
let year = date.getFullYear(); // 2020

For more detail about the javascript date get full year click here.

JavaScript Question

In this javascript quiz, find out does zero date can give get full year method as zero is not a date which is passed to date object.

First, we create a variable date using let and assign a javascript date object to a variable. And passed 0 to a date object.

let date = new Date(0);

Now, we declare an another variable year and assign a result return from getFullYear() method on date variable.

let year = date.getFullYear();

Finally, we are checking the value of year using console log.

console.log(year);

Any guess what should be the output above code statement ?

Answer – 1970

Click To Run Code

Explanation

Now, you know the answer is 1970 but why because we haven’t provided any proper date to a date object. Let me explain

Note: JavaScript date start from 01 January 1970

When 0 passes to the date object then it set to the starting date of the javascript date. So, when the getFullYear() method applied to it.

If you console the date variable you get something like.

Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)

At last, That’s why we get an answer “1970“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – string match regexp can extract a number value
  2. JS Quiz – what is the math random function range
  3. JavaScript Quiz – numbers array can be sort
  4. JavaScript Quiz – switch case can change a variable value

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link