jQuery getTime function

Tsundoku picture Tsundoku · Mar 22, 2009 · Viewed 143.7k times · Source

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...

Answer

Paolo Bergantino picture Paolo Bergantino · Mar 22, 2009

@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.