1 | 1 | 'use strict'; |
2 | | |
3 | 1 | module.exports = function(date) { |
4 | | |
5 | 6 | var type = Object.prototype.toString.call(date); |
6 | 6 | var now = new Date(); |
7 | | |
8 | | // convert dateStrings and timestamps to a Date |
9 | 6 | if (type === '[object String]' || type === '[object Number]') { |
10 | 3 | date = new Date(date); |
11 | | } |
12 | | |
13 | 6 | var yearDiff = now.getFullYear() - date.getFullYear(); |
14 | 5 | var monthDiff = now.getMonth() - date.getMonth(); |
15 | 5 | var pastDate = now.getDate() < date.getDate(); |
16 | | |
17 | 5 | if (monthDiff < 0 || (monthDiff === 0 && pastDate)) { |
18 | 2 | yearDiff--; |
19 | | } |
20 | | |
21 | 5 | return yearDiff; |
22 | | |
23 | | }; |