Formatting-date in javascript month-day-year

Wikitechy | 1707 Views | javascript | 03 Jun 2016


  • In general,we must 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 is represented by 0, February as 1, March as 2 ...
  • Its important that,we would have to increment the value returned by getmonth().

Syntax :

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

The corrected lines of code is shown below:

Sample code :  

        <title> Formatting-date </title>
            <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_month + "/" + curr_date + "/" + curr_year);

Code Explanation : 

    Here <script type="text/javascript"> 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 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 current month.

   curr_month++; it specifies that the month will be incremented by 1(since the month January starts from "0")

  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_ month + "/" + curr_date + "/" + curr_year);  document.write specifies the write() method that writes JavaScript code to a document. And (curr_month + "/" + curr_date + "/" + curr_year) returns the current month/date/year format.

   </script> defines theend of script tag in sample code 

Sample Output : 

 Here in this output display 5/30/2016 which specifies the current month/date/year.


Bug Bounty

Join our Community