Formatting-date in JavaScript date-month-year

Wikitechy | 578 Views | javascript | 03 Jun 2016

 

  • JavaScript has an inbuilt support for dates and times with its Date object. 
  • The methods of the Date object return various values associated with date and time.
  • To start working with dates and time, we first need to initialize a variable and assign it a value with the new operator and the Date() constructor. 
  • The main function of the new operator with Date() constructor is to create a new date object that is stored in the variable. 

Syntax :

var d = new Date();
  • Thus, variable d contains a new date object. We can now call the various methods of this date object.

date-month-year :

  • The three methods that we would be using are:
    • getDate(): Returns the date.
    • getMonth(): Returns the month.
    • getFullYear(): Returns the year.

Sample Code :

<html>
	<head>
		<title> Formatting-date </title>
	</head>
	<body>
             <script type="text/javascript">
                   var d = new Date();
                   var curr_date = d.getDate();
                   var curr_month = d.getMonth();
                   var curr_year = d.getFullYear();
                   document.write(curr_date + "-" + curr_month + "-" + curr_year); 
             </script>
      </body>
</html>  

Code Explanation :


   Here <script type="text/javascript"> is specifies the type attribute of the Internet media type (formerly known as MIME type) of a script. "text/javascript” specifies the media type.

    Over here var d = new Date () specifies a variable d that contains a new date object. We can now call the various methods of this date object.

    var curr_date = d.getDate() it return the current day of the month.

   var curr_month = d.getMonth() specifies The getMonth() method tahtreturns the month (from 0 to 11) for the specified date, according to local time.

   var curr_year = d.getFullYear() specifies these method that returns the year (four digits for dates between year 1000 and 9999) of the specified date.

    document.write(curr_date + "-" + curr_month + "-" + curr_year); - over here document.write specifies the write() method writes JavaScript code to a document. And (curr_date + "-" + curr_month + "-" + curr_year) returns the current date – month-year in this format.

    </script> defines the end of script tag inthe sample code.

Sample Output :


    Here in this output we display the 30-4-2016 which specifies the previous date-month-year in the format.

The corrected lines of code are:

<html>
	<head>
		<title> Formatting-date </title>
	</head>
	<body>
             <script type="text/javascript">
                   var d = new Date();
                   var curr_date = d.getDate();
                   var curr_month = d.getMonth()+ 1;
                   var curr_year = d.getFullYear();
                   document.write(curr_date + "-" + curr_month + "-" + curr_year); 
             </script>
      </body>
</html>  

  • In the code above, we have a different variable (curr_date,curr_month,curr_year) for the date, month and year. 
  • These variables all contain numeric quantities. (You can check this using typeof()). 
  • Hence, you can perform any kind of numerical operations on these variables.
  • The eagle-eyed would have noticed that the month value returned by getMonth() is one less than the current month. 
  • This is because months start at 0 value, thus, January as 0, February as 1, March as 2 ...
  • We would have to increment the value returned by getmonth().

Code Explanation :

   <script type="text/javascript"> is specifies the type attribute of the Internet media type (formerly known as MIME type) of a script. "text/javascript” specifies the media type.

    var d = new Date () specifies a variable d contains a new date object. We can now call the various methods of this date object.

    var curr_date = d.getDate() it return the current day of the month.

    var curr_month = d.getMonth()+1 specifies the current month.

   var curr_year = d.getFullYear() specifies these method returns the year (four digits for dates between year 1000 and 9999) of the specified date.

  document.write(curr_date + "-" + curr_month + "-" + curr_year); document.write specifies the write() method writes JavaScript code to a document. And (curr_date + "-" + curr_month + "-" + curr_year) returns the current date – month-year in this format.

    </script> defines the end of script tag in the sample code.

Sample Output :


    Here in this output we display the 30-5-2016 which specifies the Current date-month-year in the format.



Workshop

IOT Hackathon
Webinar

Join our Community

Advertise