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

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Difference between :where and :is in CSSCSSWhat’s the difference between variables in CSS and SCSS (Sass)?CSS3 JavaScript methods to use when you want to go back and forth in historyJavaScriptABS and NEG functions in Envision BasicEnvision BasicCSS align-items property examplesCSSColleague UI Basics: The Search AreaColleagueHow does AdSense calculate page loading time?JavaScriptDISTINCT ON: The confusing, unique and useful feature in PostgresPostgresDebugging in WordPress Part 3: SCRIPT_DEBUG and SAVEQUERIESWordPress4 ways to create Date Objects in JavaScriptJavaScriptFunction Scope in JavaScript for BeginnersJavaScriptHow to create ‘share on LinkedIn’ link using just HTML?HTML