-
Notifications
You must be signed in to change notification settings - Fork 7
/
transaction-date.js
45 lines (34 loc) · 1 KB
/
transaction-date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var parseDate = require('./lib/parse-date')
/**
* Represents a transaction date
* @constructor
* @private
*/
function TransactionDate (dateString, format, options) {
options = options || {}
var parsed = parseDate(dateString, format)
this.year = parsed.year
this.month = parsed.month
this.date = parsed.date
if (!this.year && options.succeedingDate) {
this.year = this.calculateYear(options.succeedingDate)
}
}
/**
* @returns {Date} A native Date representation of the transaction date
*/
TransactionDate.prototype.toDate = function () {
if (!Date.parse(this.year, this.month, this.date)) return null
return new Date(this.year, this.month, this.date)
}
/**
* Uses the succeeding date to determine the transaction year
* @returns {Number}
*/
TransactionDate.prototype.calculateYear = function (succeedingDate) {
var year = succeedingDate.getFullYear()
// Dec - Jan
if (succeedingDate.getMonth() === 0 && this.month === 11) year--
return year
}
module.exports = TransactionDate