article

Date

[what]

  1. [] a native js object for working with Date values, see REF 1 and 2 for some basic examples of usage
  2. [] this article shows how to add some additional properties to the native js Date object.

[why]

  1. [] consistency -
  2. [] calculations -

[where]

  1. [] money.js - implements the following example of adding additional properties (monthName, ISODate, ...) to the native js Date object and demonstrates their usage.

[example]

  1. [] adding a new property (monthName) to the native Date object ( using the Date.prototype) ?property?
  2. [] adding a new property (I 

[how-to example-1]

  1. // using the "prototype" ___ (property of the Date object) , i am adding a new function/method named "getMonthName()" to the native/primitive js object Date, this method will determine the monthName of the current Date and add/set a monthName property to the Date object
    1. Date.prototype.getMonthName=function()
      {
      if (this.getMonth()==0){this.monthName="January"};
      if (this.getMonth()==1){this.monthName="February"};
    2. // ... REPLACED the above code with an array of "monthNames" and single statement this.monthName = monthNames(this.getMonth())
    3. }
  2. CALL your new method on the instance of Date that you are working with, to set the value of your new property.
    1. var myDate = new Date();
    2. myDate.getMonthName();
  3. ACCESS the value of the new property that you have added anywhere in your js code
    1. myDate.monthName

[how-to - example-2]

  1. // using the "prototype" ___ (property of the Date object) , i am adding a new function/method named "getISO()" to the native/primitive js object Date, this method will determine the monthNumber of the current Date and set an ISODate format property ( YES  i know there is a native js method for this, BUT it doesnt appear to work in all browsers(IE8))
    1. Date.prototype.getISO=function()
    2. // created an array of "monthNumbers"
    3. // used getFullYear().toString and this.getDate() to set Year and Day values 
    4. SET this.ISO_8601 property = year+monthNumbers(this.getMonth())+day
    5. }
  2. CALL your new method on the instance of Date that you are working with, to set the value of your new property.
    1. var date = new Date();
    2. date.getISO();
  3. ACCESS the value of the new property that you have added anywhere in your js code
    1. date.ISO_8601

[reference]

  1. ] http://www.w3schools.com/jsref/jsref_obj_date.asp -
  2. ] http://www.javascriptkit.com/jsref/date.shtml -
  3. ] scripts/money.js - full source code
  4. ] CC to - js-OOP -
Details Photos Edit more

Details

ID: 1677

NAME: Date

DESCRIPTION: Date - the native JavaScript language "Date" object

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -5

OWNER ID: 1

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and