Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From the Definitive Guide (4th Ed.):

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •