-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDate.js
97 lines (78 loc) · 2.99 KB
/
CDate.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
const DateUtil = require("./DateUtil")
class CDate {
constructor(year = 1970, month = 1, day = 1, hour = 0, minute = 0, second = 0, milli = 0) {
this.year = parseInt(year);
this.month = parseInt(month);
this.day = parseInt(day);
this.hour = parseInt(hour);
this.minute = parseInt(minute);
this.second = parseInt(second);
this.milli = parseInt(milli);
}
isLeapYear() {
return DateUtil.CDate.isLeapYear(this.year);
}
getDaysInMonth() {
return DateUtil.CDate.getDaysInMonth(this.month, this.year);
}
formattedSum(date) {
var yearOut = this.year + date.year;
if (this.year > 0 && this.year + date.year <= 0) yearOut -= 1;
else if (this.year < 0 && this.year + date.year >= 0) yearOut += 1;
return DateUtil.CDate.reformatDate(new CDate(yearOut, this.month + date.month, this.day + date.day, this.hour + date.hour, this.minute + date.minute, this.second + date.second, this.milli + date.milli));
}
sum(date) {
return new CDate(this.year + date.year, this.month + date.month, this.day + date.day, this.hour + date.hour, this.minute + date.minute, this.second + date.second, this.milli + date.milli)
}
formattedDiff(date) {
var yearOut = this.year - date.year;
if (this.year > 0 && this.year - date.year <= 0) yearOut -= 1;
else if (this.year < 0 && this.year - date.year >= 0) yearOut += 1;
return DateUtil.CDate.reformatDate(new CDate(yearOut, this.month - date.month, this.day - date.day, this.hour - date.hour, this.minute - date.minute, this.second - date.second, this.milli - date.milli));
}
diff(date) {
return new CDate(this.year - date.year, this.month - date.month, this.day - date.day, this.hour - date.hour, this.minute - date.minute, this.second - date.second, this.milli - date.milli)
}
localDate(offset) {
var hour = Math.floor(offset);
var minute = Math.floor((offset-hour) * 60);
return this.formattedSum(new CDate(0, 0, 0, hour, minute, 0, 0));
}
getValue(intIn, length) {
var int = intIn + "";
var out = "";
if (int.length < length) {
var l = length - int.length;
for(var i = 0; i < l; i++) {
out += "0";
}
}
out += int;
return out;
}
getYear() {
return this.year;
}
getMonth() {
return this.month;
}
getDay() {
return this.day;
}
getHour() {
return this.hour;
}
getMinute() {
return this.minute;
}
getSecond() {
return this.second;
}
getMilli() {
return this.milli;
}
toString() {
return (this.year + ":" + this.getValue(this.month, 2) + ":" + this.getValue(this.day, 2) + ":" + this.getValue(this.hour, 2) + ":" + this.getValue(this.minute, 2) + ":" + this.getValue(this.second, 2) + ":" + this.getValue(this.milli, 3));
}
}
module.exports = CDate;