On Apr 14, 8:58 am, Klaus Hartl <klaus.ha...@googlemail.com> wrote:
> On 14 Apr., 15:41, MorningZ <morni...@gmail.com> wrote:
>
> > So what is the error
>
> The error is obviously that toDate is not a jQuery method. Another

Right.

> problem is that that Dreamweaver method is using document.write which
> can cause a problem depending on where the function is being called
> and which is usually avoided in jQuery land.

Calling document.write in a jQuery ready callback won't work
consistently.

>
> Since toDate is not (yet) a jQuery method, let's make one:
>
> (function($) {
>
>     var days = [
>         'Sunday',
>         'Monday',
>         'Tuesday',
>         'Wednesday',
>         'Thursday',
>         'Friday',
>         'Saturday'
>     ];
>
>     var months = [
>         'January',
>         'February',
>         'March',
>         'April',
>         'May',
>         'June',
>         'July',
>         'August',
>         'September',
>         'October',
>         'November',
>         'December'
>     ];
>
>     $.fn.toDate = function() {
>         var now = new Date;
>         return this.html('<p>Today\'s Date:<br />' + days[now.getDay
> ()] +
>                         ', ' + now.getDate() + ' ' + months[now.getMonth()] + 
> ' ' +
>                         now.getFullYear() + '</p>');
>     };
>
> })(jQuery);
>
> Now you can use it like:
>
> $(document).ready(function() {
>    $('#today').toDate();
>
> });
>

Or not. The above is fine for some locales, but not all.

If you want to format a local date, what is wrong with just having:-

var todayEl = document.getElementById("today");
todayEl.innerHTML = formatDate(new Date); // [1]

?

[1]http://www.jibbering.com/faq/#formatDate

Garrett

Reply via email to