forked from egorFiNE/node-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraDate.js
More file actions
211 lines (133 loc) · 4.05 KB
/
ExtraDate.js
File metadata and controls
211 lines (133 loc) · 4.05 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require('date-utils');
function _zeroStrip(s) {
return s.toString().replace(/^0+/, '');
}
/**
Various date methods useful for stock trading.
Yes, I know that it is not good to augment built-in types, but I have considered all pros and cons and
decided to pick this solution anyway.
A **daystamp** is a date format in form of "YYYYMMDD". It's used through all the node-stock package. It could
be specified both in string or integer and all the methods and functions must accept either type
(mind <code>.toString()</code> and <code>parseInt()</code> for easy implementation).
*/
// trick doc.js into thinking that we have declared the Date class.
/*
function Date() {
};
*/
/**
Parse daystamp into Date. It's a factory method.
@param daystamp daystamp to parse
@return {Date} date parsed from daystamp, time set to 00:00:00
*/
Date.parseDaystamp = function(daystamp) {
daystamp = daystamp.toString();
var y = parseInt(daystamp.substr(0,4));
var m = parseInt(_zeroStrip(daystamp.substr(4,2)));
var d = parseInt(_zeroStrip(daystamp.substr(6,2)));
m--;
return new Date(y,m,d);
};
/**
Return daystamp representation of a Date.
@return {String} daystamp
*/
Date.prototype.daystamp = function() {
return this.toFormat('YYYYMMDD');
};
/**
Parse unixtime into Date. It's a factory method.
@param {Integer} unixtime unixtime to parse.
@return {Date} parsed date object with correct time set.
*/
Date.parseUnixtime = function(unixtime) {
return new Date(unixtime*1000);
};
/**
Return unixtime representation of a Date.
@return {Integer} unixtime.
*/
Date.prototype.unixtime = function() {
return parseInt(this.getTime()/1000);
};
/**
Return current unixtime.
@return {Integer} current unixtime.
*/
Date.unixtime = function() {
return new Date().unixtime();
};
/**
Return an array of Dates (days) between two dates, inclusive.
@param {Date} from starting day
@param {Date} to ending day
@return {Array} array of Dates between those.
If from and to are the same, it will only return one element.
Example:
var from = new Date('2011-06-15');
var to = new Date('2011-06-18');
var days = Date.fillEmptyDays(from, to);
// days == [
new Date('2011-06-15'),
new Date('2011-06-16'),
new Date('2011-06-17'),
new Date('2011-06-18')
];
Note: this function is not time-safe, so please use only for dates.
*/
Date.fillEmptyDays = function(from, to) {
var result=[];
var nextDate = new Date(from);
nextDate.clearTime();
var firstDate = new Date(nextDate);
result.push(nextDate);
nextDate = Date.parseUnixtime(nextDate.unixtime()+86400);
to = new Date(to);
to.clearTime();
while (nextDate < to) {
result.push(nextDate);
nextDate = Date.parseUnixtime(nextDate.unixtime()+86400);
}
if (firstDate.daystamp()!=to.daystamp()) {
result.push(to);
}
return result;
};
/**
Get short human readable day name for given Date, like "Sun", "Mon", ...
@return {String} day name.
*/
Date.prototype.getDayName = function() {
return ['Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun'][this.getDay()];
};
/**
Set given Date's time to a day minute specified. A day minute is zero-based number of minute in a day,
e.g. 570 for 9:30.
@param {Integer} minute day minute to set as time.
*/
Date.prototype.setCurrentDayMinute = function(minute) {
var hours = Math.floor(minute/60);
var minutes = minute-hours*60;
this.setHours(hours, minutes, 0, 0);
};
/**
Get current day minute of a given Date. A day minute is zero-based number of minute in a day,
e.g. 570 for 9:30.
@return {Integer} day minute
*/
Date.prototype.getCurrentDayMinute = function() {
return this.getHours()*60 + this.getMinutes();
};
/**
A helper function. Format given day minute according to format (see date-utils format specifiers).
@param {Integer} minute day minute
@param {String} format date-utils format specifier
@return {String} formatted time
Example:
Date.minuteToFormat(571, 'H24:MI:SS'); // "9:31"
*/
Date.minuteToFormat = function(minute, format) {
var d = new Date();
d.setCurrentDayMinute(minute);
return d.toFormat(format);
};