How to use Date Prototype in JavaScript

In this article I have described about Date Prototype property used in JavaScript.
  • 1390

Date Prototype property of JavaScript

  • As we know that the Date object is used to work with dates and times.
  • If we want to add new properties and methods to the Date object, we can use prototype property.

Browser support

Following are the main browsers which support the prototype property of Date object

  • Internet Explorer.
  • Firefox.
  • Opera.
  • Google Chrome.
  • Safari.

Syntax

The syntax of prototype property of Date object as follows

Date.prototype.name=value

Lets take an example

<!DOCTYPE html>

<html>

<body style ="background-color:green">

<p id="menu">DISPLAY THE DAY'S NAME WITH DATE OBJECT </p>

<button onclick="myFunction()">CLICK</button>

<script type="text/javascript">

       Date.prototype.myMet = function ()

{

    if (this.getDay() == 0) { this.myProp = "Sunday" };

    if (this.getDay() == 1) { this.myProp = "Monday" };

    if (this.getDay() == 2) { this.myProp = "Tuesday" };

    if (this.getDay == 3) { this.myProp = "Wesnesday" };

    if (this.getDay() == 4) { this.myProp = "Thrusday" };

    if (this.getDay() == 5) { this.myProp = "Friday" };

    if (this.getDay() == 6) { this.myProp = "Saturday" };

}

    function myFunction() {

        var g = new Date();

        g.myMet();

        var x = document.getElementById("menu");

        x.innerHTML = g.myProp;

    }

</script>

</body>

</html>

 

Output


When we run the above program

date prototype.jpg


When we click on button


date prototype.jpg2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.