Home >>Javascript Programs >Get current date, month and year in JavaScript

Get current date, month and year in JavaScript

Get current date, month and year in JavaScript

In this article, we will learn how to get current date, month and year by using JavaScript?

You see many of the websites prefer Date and Time for providing interaction like event based websites, school & college’s websites, etc. JavaScript helps you to built in Date object. This article will help you to get current date, month and year by using JavaScript easily.

Methods

  • By using new Date(), we get the current date.
  • To get the current day in a number format, you can use currentDate.getDay()
  • To get the current month in a number format, you can use currentDate.getMonth().

We can create a date object by using the Date() constructor. By using the new operator we will define a variable today, it helps to create a date object of today’s date.

Here is a Date object methods available in JavaScript to get the current the current Date, current month and current year.

Let's take an example:

<!DOCTYPE HTML> 
<html> 
<head> 
	<title></title> 
</head> 
<body> 
	<p id="php_UP"></p> 
	<button onclick="php_Run()"> 
		Click Me 
	</button> 
	<p id="php_DOWN"></p> 
	<script> 
		var el_up = document.getElementById("php_UP"); 
		var el_down = document.getElementById("php_DOWN"); 
		var today = new Date(); 
		var dd = today.getDate(); 
		var mm = today.getMonth() + 1; 
		var yyyy = today.getFullYear(); 
		if (dd < 10) { 
			dd = '0' + dd; 
		} 
		if (mm < 10) { 
			mm = '0' + mm; 
		} 
		var today = dd + '/' + mm + '/' + yyyy; 
		function php_Run() { 
			el_down.innerHTML = today; 
		} 
	</script> 
</body> 
</html>
Output:

 

 


No Sidebar ads