JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does get date and month from date object is same

JavaScript Quiz – does get date and month from date  object is same

Get date and month from date

In javascript, playing with date object is normal, and getting required detail from a date like a date, month, year, etc is done by date get methods. JavaScript date has a single moment of time.

The date object get method also helps in creating any kind of date format like dd-mm-yyyy, mm-dd-yyyy, etc.

Basic Syntax of Date

Date()

For more detail about javascript Date click here.

JavaScript Question

In this javascript quiz, find out does get date and month from date object is same because date get method gives detail from the date object.

First, we declare a variable date and using let keyword and assign a date object to it with the date “2020-10-15“.

let date = new Date("2020-10-15");

In the next step, we get the Date detail from the date object using the getDate() method and check its value using the console log.

console.log(date.getDate());

Now, we also getting the month of date using getMonth() method.

console.log(date.getMonth());

Any idea, what should be the output of above code statements.

JavaScript Quiz Test

Here is the full code snippet for you to solve.

let date = new Date("2020-10-15");

console.log(date.getDate());
// output => ?

console.log(date.getMonth());
// output => ?

Answer – 15, 9

Click To Run Code

Explanation

Now, you know the answer is 15, 9. But why, doesn’t month value should be 10. Let me explain.

So, for getDate() method it give 25 which is clearly visible in date which pass to Date(“2020-10-15”).

But for getMonth() method always give one value less. That means, if you pass a date with month (10) “2020-10-15″.

Then it will give 9. Here is a solution snippet.

let date = new Date("2020-10-15");

console.log(date.getDate());
// output => 15

console.log(date.getMonth());
// output => 9

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

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 – Math random in if condition
  2. JS Quiz – three dots with string variable will expand
  3. JavaScript Quiz – solve 2d array matrix diagonal addition
  4. JavaScript Quiz – find numbers difference with math abs

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