-1

Possible Duplicate:
How to calculate the number of days between two dates using JavaScript?

I wonder with the current date, how can I get the age in days. For example at date 12/4/1995 returned 6435.

Is there any library function that does this?

Community
  • 1
  • 1
John Mauric
  • 15
  • 1
  • 3

1 Answers1

4

You can compose two dates and subtract them. The result will be in milliseconds, so you have to convert that to days:

var days = (new Date() - new Date(1995, 11, 4)) / (1000 * 60 * 60 * 24);
//            (today)          (then)             (milliseconds per day)

You can then round days (and as appropriate.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Allowing Date to parse strings into dates is not a good idea, much better to provide explicit values: `new Date(1995,11,4)`. The format you've provided is not specified in ECMA-262 and how it's parsed is implementation dependent. Also, the [specified format](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15) (a version of ISO8601) is not supported by all browsers in use. – RobG Nov 23 '12 at 22:13