-
Notifications
You must be signed in to change notification settings - Fork 4
/
dateonly.js
123 lines (95 loc) · 2.99 KB
/
dateonly.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function(name, definition) {
if (typeof module !== 'undefined') module.exports = definition();
else if (typeof define === 'function' && typeof define.amd === 'object') define(definition);
else this[name] = definition();
}('DateOnly', function() {
"use strict";
function DateOnly(date) {
var val = null;
if (date instanceof String || typeof date === 'string') date = new Date(date);
if (date instanceof Number || typeof date === 'number') date = numberToDate(date);
if (date && date.constructor && date.constructor.name === 'DateOnly') date = date.toDate();
if (date && date.constructor && date.constructor.name === 'Moment') date = momentToDate(date);
if (date instanceof Date) val = date;
this.saveDateOnly(val || new Date());
}
DateOnly.toDate = function(dateOnlyStamp) {
return new DateOnly(dateOnlyStamp).toDate();
};
DateOnly.prototype.saveDateOnly = function(date) {
this.date = date.getDate();
this.month = date.getMonth();
this.year = date.getFullYear();
};
DateOnly.prototype.getDate = function() {
return this.date;
};
DateOnly.prototype.setDate = function(date) {
this.date = date;
};
DateOnly.prototype.getMonth = function() {
return this.month;
};
DateOnly.prototype.setMonth = function(month) {
this.month = month;
};
DateOnly.prototype.getFullYear = function() {
return this.year;
};
DateOnly.prototype.setFullYear = function(year) {
this.year = year;
};
DateOnly.prototype.getDay = function() {
return this.toDate().getDay();
};
DateOnly.prototype.toDate = function() {
return partsToDate(this.year, this.month, this.date);
};
DateOnly.prototype.valueOf = function() {
if (isNaN(this.year) || isNaN(this.month) || isNaN(this.date)) {
return NaN;
}
return parseInt(pad(this.year, 4) + pad(this.month, 2) + pad(this.date, 2), 10);
};
DateOnly.prototype.toString = function() {
return this.toDate().toDateString();
};
DateOnly.prototype.toISOString = function() {
return pad(this.year, 4) + '-' + pad(this.month + 1, 2) + '-' + pad(this.date, 2);
}
DateOnly.prototype.toJSON = function() {
return this.valueOf();
};
function pad(num, length) {
num = num.toString();
while (num.length < length) {
num = '0' + num;
}
return num;
}
function numberToDate(n) {
var s = n.toString();
if (s.length === 8) {
var year = s.slice(0, 4);
var month = s.slice(4, 6);
var day = s.slice(6, 8);
return partsToDate(year, month, day);
} else {
return new Date(n);
}
}
function momentToDate(m) {
var dateStamp = +m.format('YYYYMMDD');
dateStamp -= 100; // moment's months start at 1, but DateOnly start at 0, so subtract 1 month.
return numberToDate(dateStamp);
}
function partsToDate(year, month, day) {
var date = new Date(year, month, day);
date.setHours(0, 0, 0, 0);
return date;
}
function isNaN(v) {
return v !== v;
}
return DateOnly;
}));