JavaScript Date prototype properties

Definition and Usage

prototype The constructor allows you to add new properties and methods to the Date() object.

When constructing properties, all date objects will be assigned properties and their values as default values.

All date objects can use this method in the constructor.

Note:Date.prototype does not refer to a single date object, but to the Date() object itself.

Note:Prototype is a global object constructor that applies to all JavaScript objects.

Instance

Create a new date method, providing a month-name property named myProp for the date object:

Date.prototype.myMet = function() {
  if (this.getMonth() == 0){this.myProp = "January"};
  if (this.getMonth() == 1){this.myProp = "February"};
  if (this.getMonth() == 2){this.myProp = "March"};
  if (this.getMonth() == 3){this.myProp = "April"};
  if (this.getMonth() == 4){this.myProp = "May"};
  if (this.getMonth() == 5){this.myProp = "June"};
  if (this.getMonth() == 6){this.myProp = "July"};
  if (this.getMonth() == 7){this.myProp = "August"};
  if (this.getMonth() == 8){this.myProp = "September"};
  if (this.getMonth() == 9){this.myProp = "October"};
  if (this.getMonth() == 10){this.myProp = "November"};
  if (this.getMonth() == 11){this.myProp = "December"};
;

Create a Date object and then call the myMet method:

var d = new Date();
d.myMet();
var monthname = d.myProp;

Try It Yourself

Syntax

Date.prototype.name = value

Technical Details

JavaScript Version: ECMAScript 1

Browser Support

Properties Chrome IE Firefox Safari Opera
prototype Support Support Support Support Support

Related Pages

Tutorial:JavaScript Date

Tutorial:JavaScript Date Format

Tutorial:JavaScript Object Constructor