Search code examples
jquerygettime

jQuery getTime function


is it possible to create a jQuery function so that it gets current date and time? I've been looking around documentation but haven't found anything so far...


Solution

  • @nickf's correct. However, to be a little more precise:

    // if you try to print it, it will return something like:
    // Sat Mar 21 2009 20:13:07 GMT-0400 (Eastern Daylight Time)
    // This time comes from the user's machine.
    var myDate = new Date();
    

    So if you want to display it as mm/dd/yyyy, you would do this:

    var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
    

    Check out the full reference of the Date object. Unfortunately it is not nearly as nice to print out various formats as it is with other server-side languages. For this reason there-are-many-functions available in the wild.