1 |
|
2 |
|
3 |
|
4 |
|
5 | var _ = require('lodash'),
|
6 | SchemaType = require('../schematype');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var SchemaDate = module.exports = function(options){
|
16 | SchemaType.call(this, options);
|
17 | };
|
18 |
|
19 | SchemaDate.type = SchemaDate.prototype.type = Date;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | SchemaDate.__proto__ = SchemaType;
|
26 | SchemaDate.prototype.__proto__ = SchemaType.prototype;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | SchemaDate.prototype.checkRequired = function(value){
|
37 | return value instanceof Date;
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var cast = SchemaDate.prototype.cast = function(value){
|
49 | if (value == null || value === '') return null;
|
50 | if (value instanceof Date) return value;
|
51 |
|
52 | if (value instanceof Number || typeof value === 'number'){
|
53 | return new Date(value);
|
54 | }
|
55 |
|
56 | if (!isNaN(+value)){
|
57 | return new Date(+value);
|
58 | }
|
59 |
|
60 | var date = new Date(value);
|
61 |
|
62 | if (date.toString() !== 'Invalid Date'){
|
63 | return date;
|
64 | } else {
|
65 | return null;
|
66 | }
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | SchemaDate.prototype.save = function(value){
|
78 | return value.valueOf();
|
79 | };
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | SchemaDate.prototype.compare = function(data, value){
|
91 | return data.valueOf() === cast(value).valueOf();
|
92 | };
|
93 |
|
94 | SchemaDate.prototype.q$year = function(data, value){
|
95 | return data ? data.getFullYear() == value : false;
|
96 | };
|
97 |
|
98 | SchemaDate.prototype.q$month = function(data, value){
|
99 | return data ? data.getMonth() == value - 1 : false;
|
100 | };
|
101 |
|
102 | SchemaDate.prototype.q$day = function(data, value){
|
103 | return data ? data.getDate() == value : false;
|
104 | };
|
105 |
|
106 | SchemaDate.prototype.u$inc = function(data, value){
|
107 | if (!data) return;
|
108 |
|
109 | return new Date(data.valueOf() + +value);
|
110 | };
|
111 |
|
112 | SchemaDate.prototype.u$dec = function(data, value){
|
113 | if (!data) return;
|
114 |
|
115 | return new Date(data.valueOf() - +value);
|
116 | }; |
\ | No newline at end of file |