dots Created with Sketch.
Yogesh Chauhan's Blog

How to get Current Year, Month and Date in JavaScript?

in JavaScript on August 13, 2020

Using JavaScript Date Objects we can get the current year, month and date as well as manipulate those as well. But that's not the talking point in this post.

Get Year

The getFullYear() method returns the full year of any specified date.

So, we can create a date object first and then using that date we can get the year.


var d = new Date();
console.log(d.getFullYear());

// 2020

Get Month

The getMonth() method returns the month of a date as a number from 0 to 11.

So, similarly like year, we can create a date object first and then using that date we can get the month.

But, we need to add 1 to the number as we will get the month from 0 to 11.


var d = new Date();
console.log(d.getMonth()+1);

// 8

Get Date

The getDate() method returns the day of a date as a number between 1 to 31.

So, similarly like year and month, we can create a date object first and then using that date we can get the day.


var d = new Date();
console.log(d.getDate());

// 14
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
Debugging in WordPress Part 1: WP_DEBUGWordPressReading Multiple Inputs in SwiftSwiftHow does AdSense calculate page loading time?JavaScriptWhat does useEffect do in React?ReactHow to use HTML picture Tag?HTMLHow to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLSocial Media Colors: LESS VariablesMiscellaneousSorting Arrays in JavaScriptJavaScriptJavaScript Number MethodsJavaScript3 JavaScript methods to use when you want to go back and forth in historyJavaScriptA list of wp-cli commands to use via SSHWordPressHow to create a flip pricing table using CSS and JavaScript?CSS