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
How to create a Recent Posts function in WordPress?WordPressHow does Binding work in JavaScript?JavaScriptDid you get an error while displaying Special Characters in JavaScript?JavaScriptMySQL queries to get data rows from previous month as well as from last 30 daysSQL/MySQLColumn and Table Alias in PostgresPostgresA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneousJavaScript Input Validation Theme Park Example using throw StatementJavaScriptHow Do You Make a Private VPN?MiscellaneousSQL Inner JoinSQL/MySQLHow to use HTML picture Tag?HTMLWordPress: How to display slider repeater fields in ACF?WordPressHow to create a smooth scrolling effect with CSS?CSS