edit-article
Home
Up
Delete
Article Name:
Article Description:
Date - the native JavaScript language "Date" object
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">Date</h1> <h2>[what]</h2> <ol> <li>[] a native js object for working with Date values, see REF 1 and 2 for some basic examples of usage</li> <li>[] this article shows how to add some additional properties to the native js Date object.</li> </ol> <h2>[why]</h2> <ol> <li>[] consistency -</li> <li>[] calculations -</li> </ol> <h2>[where]</h2> <ol> <li>[] money.js - implements the following example of adding additional properties (monthName, ISODate, ...) to the native js Date object and demonstrates their usage.</li> </ol> <h2>[example]</h2> <ol> <li>[] adding a new property (monthName) to the native Date object ( using the Date.prototype) ?property?</li> <li>[] adding a new property (I </li> </ol> <h2>[how-to example-1]</h2> <ol> <li>// 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</li> <ol> <li>Date.prototype.getMonthName=function()<br />{<br />if (this.getMonth()==0){this.monthName="January"};<br />if (this.getMonth()==1){this.monthName="February"};</li> <li>// ... REPLACED the above code with an array of "monthNames" and single statement this.monthName = monthNames(this.getMonth())</li> <li>}</li> </ol> <li>CALL your new method on the instance of Date that you are working with, to set the value of your new property.</li> <ol> <li>var myDate = new Date();</li> <li>myDate.getMonthName();</li> </ol> <li>ACCESS the value of the new property that you have added anywhere in your js code</li> <ol> <li>myDate.monthName</li> </ol></ol> <h2>[how-to - example-2]</h2> <ol> <li>// 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))</li> <ol> <li>Date.prototype.getISO=function()</li> <li>// created an array of "monthNumbers"</li> <li>// used getFullYear().toString and this.getDate() to set Year and Day values </li> <li>SET this.ISO_8601 property = year+monthNumbers(this.getMonth())+day</li> <li>}</li> </ol> <li>CALL your new method on the instance of Date that you are working with, to set the value of your new property.</li> <ol> <li>var date = new Date();</li> <li>date.getISO();</li> </ol> <li>ACCESS the value of the new property that you have added anywhere in your js code</li> <ol> <li>date.ISO_8601</li> </ol></ol> <h2>[reference]</h2> <ol> <li>] <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">http://www.w3schools.com/jsref/jsref_obj_date.asp</a> -</li> <li>] <a href="http://www.javascriptkit.com/jsref/date.shtml">http://www.javascriptkit.com/jsref/date.shtml</a> -</li> <li>] scripts/money.js - full source code</li> <li>] CC to - js-OOP -</li> </ol>