Digital Web Magazine

The web professional's online magazine of choice.

Extending The JavaScript Date Object with User Defined Methods : Comments

By Lawrence O'Sullivan

March 4, 2008

Comments

Rikk Mo

March 5, 2008 3:28 AM

Great source code tutorial.

What would be better is if you had a demo somewhere to show the actual methods used. It makes it easier to understand the steps involved if we know what the end result looks like.

Henry

March 5, 2008 4:41 AM

Great Work, Lawrence!!

Lawrence

March 7, 2008 8:54 AM

Thanks for the comments

Rikk Mo — Let me check with the editors and see if something can be done about a demo.

shms

March 9, 2008 8:42 PM

is it possible to used in C#?

Lawrence

March 10, 2008 8:48 AM

shms — I don’t do a lot with .net or c# so my response is limited to speculation.

The code is JavaScript and designed to run on the client. This, in part, excludes c#, which runs on the server. That said, if you are using the server-side code to generate JavaScript—what I think Microsoft refers to as code behind—then there is the possibility of fitting this into the .net paradigm. Otherwise, the .js file has to added to the (x)html page with a script tag in the traditional manner. And, this can be done with .net.

Perhaps someone else has more insight on this?

Matthew Pennell

March 10, 2008 1:02 PM

@shms: If you’re asking whether it is possible to extend a base object with additional methods, it is possible with C#, but not at runtime. As C# is compiled, you would have to create the new methods within a class that extends the base class (I guess, I don’t have any direct experience with the language). But it is certainly possible to extend objects in any object-oriented language.

Andrew Gilfrin

March 14, 2008 2:02 PM

I find the way you apply Prototype to JavaScript quite nice as it moves more to the Objected Oriented Programming. Definitely cleaner to read.

Rohil Sinha

March 16, 2008 12:16 AM

Hey Lawrence,

That was such a fantastic and useful read. Thanks !

Rohil

Matthew Pennell

March 16, 2008 5:03 AM

Lawrence has kindly put together an interactive demo of all the functions listed in the article – you can find it here:

JavaScript Date Object Demo

Chris Knowles

March 18, 2008 4:06 AM

Personally I disagree with tampering with the Javascript language. It is better IMO to namespace all your code to avoid clashes with other code that it may run alongside. I have personal experience of the problems caused when I’ve had to add my own js app to a page that was already using some other js that extended the Array prototype and caused my code to break. My code ran fine on it’s own, just not alongside this other code.

Lawrence

March 18, 2008 10:12 AM

Chris –

Your point is well taken. There can be risks when extending core JavaScript objects.

A few years back there were issues with a third party library and the use of the “in” operator to iterate an array. They did that as away to avoid processing undefined array elements. But, then extensions to arrays where selected.

Current thinking is the “in” operator should not be used to step through an array.

I think extending objects, both core objects and user objects, is not tampering but using JavaScript features as intended. Albeit, there can be unintended consequences as in any code that need to be considered.

The methods provided are low risk because they don’t impact customary use of the Date object and methods.

geoffrey.mcgill

March 18, 2008 12:52 PM

Hi Lawrence,

We’ve been working on a JavaScript Date library for a while that you (and/or your readers) might be interested in checking out.

(Datejs) http://www.datejs.com/

I would also encourage checking out the TimeSpan and TimePeriod classes, included within the time.js file. The TimeSpan object simplifies a lot of the functionality you document regarding getting the difference between dates/times.

We also provide support for 150+ languages.

The Parser is pretty cool….

Date.parse(“next friday”);

Hope this helps.

lawrence

March 18, 2008 3:01 PM

Hi Geoffrey,

I just downloaded and took a quick look at your library (the SVN source retains formatting and can be read). It certainly has an extensive list of features. And, impressive work adding the language support.

I see you’re overriding the built-in static parse method with your custom method. I think that has some risks as Chris was pointing out.

The library has some good ideas. I can see it being useful in a calendar application.

Excellent work!

Kiran

March 26, 2008 4:53 AM

Hey Lawrence,

That was such a fantastic and useful read. Thanks !

Can you help me to show the saturday of current week with the help of java script.

The date should be change automatically when next saturday will come.

Please !

Thanks
Kiran

Lawrence

March 28, 2008 3:23 PM

Hi Kiran,

Thanks for the compliment.

Getting Saturday of the current week is easy since it is the last day of the week. We just add the number of days needed to reach Saturday to the current date. Here is a basic code:

var saturday = new Date(); //today
saturday.setDate(saturday.getDate() + 6 – saturday.getDay()); //Saturday

The variable saturday will be Saturday of the week containing “new
Date().”

getDay returns the day of week 0 – 6. The number of days to add to reach Saturday of the same week is 6 – the current day or 6 – date.getDay().

And we add days by adding the number of days to the day of month—getDate()—and setting the date to that number with setDate(). If the results exceeds the number of days in the month, JavaScript will automatically go into the next month.

We can extend the date constructor like this:

Date.prototype.setSaturdayThisWeek = function() {
this.setDate(this.getDate() + 6 – this.getDay());
};

If you want a method that returns a new date preserving the original date, then create a copy before finding Saturday. For example in basic code:

var today = new Date(); //today
var saturday = new Date(today.getTime());

or use the copy method in the article
var saturday = today.copy();

saturday.setDate(saturday.getDate() + 6 – saturday.getDay()); //Saturday

And the constructor can be extended like this:

Date.prototype.getSaturdayThisWeek = function() {
var tempDate = this.copy();
tempDate.setDate(tempDate.getDate() + 6 – tempDate.getDay());
return tempDate;
};

I hope this helps

Lawrence

johnsy

June 26, 2008 5:44 AM

The addMonth() method is not working for me, its giving me the alert message “indefined”, can someone help me in this.

Johnsy

June 28, 2008 11:32 PM

The javascript library for date object is really wonderful.The addday.month,year is wow, but unfortunately the Addmonth is not working for me, the alert is giving me the “undefined” if i try to pass the month as parameter to the addMonth method to add the month, all the other methods are working great except addMonth,day,year.

Can you please give me the solution to implement the addMonth().

Oleg

July 13, 2008 8:08 PM

Thank You, Mr. Lawrence, for excelent tutorial. It is a very helpfull source

Sorry, comments are closed.

Media Temple

via Ad Packs