Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2003, 03:28 PM   PM User | #1
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
Quickie Date convenience method

Code:
Date.prototype.getUTC = function () {
    return Date.UTC(
        this.getFullYear(), 
        this.getMonth(), 
        this.getDate(), 
        this.getHours(), 
        this.getMinutes(), 
        this.getSeconds(), 
        this.getMilliseconds()
    );
}
Just like any prototype method, call like this:
Code:
var myDate = new Date();
myDate.getUTC();
Enjoy. Hope you get some use out of it.
Choopernickel is offline   Reply With Quote
Old 09-25-2003, 08:12 PM   PM User | #2
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
Hmmm....this line:

Code:
return Date.UTC
...would return the current time, yes? So shouldn't it be in accordance with the particular Date object?

Code:
return this.UTC
I'm not sure about this, but I think so.

Happy coding!
nolachrymose is offline   Reply With Quote
Old 09-25-2003, 09:20 PM   PM User | #3
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
From the Definitive Guide (4th Ed.):

Quote:
Date.UTC() is a static method; it is invoked through the Date() constructor, not through an individual Date object.
...
To create a Date object using a UTC time specification, you can use code like this:
d = new Date(Date.UTC(1996, 4, 8, 16, 30));
Basically, it's not something where you can say

Code:
var myDate = new Date();
myDate.UTC()
and have it do anything good.

The Date.UTC() method requires arguments to work properly; passing it the arguments from this effectively makes
Code:
return Date.UTC(
    this.getFullYear(), 
    this.getMonth(), 
    this.getDate(), 
    this.getHours(), 
    this.getMinutes(), 
    this.getSeconds(), 
    this.getMilliseconds()
);
act on the specific date object.

You should note that the method is collapsible to three lines, including a line for the close brace. I split each argument onto its own line for readability and to prevent side-scrolling in normal-sized windows.
Choopernickel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.