egret(白鹭引擎) 获取,本周,上周,本月,下月,本季,上季,今天,昨天,明天,开始时间和结束时间
ChrisXie Lv5

egret项目基本是用TS代码写,但是业务逻辑方面和JavaScript差不多,刚好有一个报表项目需要根据时间来查询数据所以结合了JavaScript的写法转换成TS

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
211
212
213
214
215
216
217
218
219
220
class ReportTime extends  egret.DisplayObjectContainer{

private now = new Date(); //当前日期
private nowDayOfWeek = this.now.getDay(); //今天本周的第几天
private nowDay = this.now.getDate(); //当前日
private nowMonth = this.now.getMonth(); //当前月
private nowYear = this.now.getFullYear(); //当前年
// private lastMonthDate = new Date(); //上月日期
// private lastYear = this.lastMonthDate.getFullYear();
// private lastMonth = this.lastMonthDate.getMonth();

public constructor() {
super();
// this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addToStage,this);
}
private addTiem(){
this.nowYear += (this.nowYear < 2000) ? 1900 : 0;
// this.lastMonthDate.setDate(1);
// this.lastMonthDate.setMonth(this.lastMonthDate.getMonth()-1);
}
//格式化日期:yyyy-MM-dd
public formatDate(date) {
let myyear = date.getFullYear();
let mymonth = date.getMonth()+1;
let myweekday = date.getDate();

if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
return (myyear+"-"+mymonth + "-" + myweekday);
}

public formatReportDate(dates,specific){
dates = new Date(dates);
console.log(dates);
let myyear = dates.getFullYear();
let mymonth = dates.getMonth()+1;
let myweekday = dates.getDate();
let myday = dates.getDay();
let hh = dates.getHours(); //时
let mm = dates.getMinutes(); //分
let ss = dates.getSeconds(); //秒


if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
if(specific == 1){
return (mymonth + "-" + myweekday + " "+GlobalVariable.getLangDay(myday) +" "+hh+":"+mm+":"+ss);
}
return (mymonth + "-" + myweekday + " "+GlobalVariable.getLangDay(myday) );
}

public getCurrentDate(){
let mymonth = this.now.getMonth()+1;
let myweekday = this.now.getDate();
if(mymonth < 10){
mymonth = <any>"0" + mymonth;
}
if(myweekday < 10){
myweekday = <any>"0" + myweekday;
}
let time = this.now.getFullYear()+"-"+mymonth+"-"+myweekday;
return time;
}

//获得某月的天数
public getMonthDays(myMonth){
this.addTiem();
let monthStartDate:any = new Date(this.nowYear, myMonth, 1);
let monthEndDate:any = new Date(this.nowYear, myMonth + 1, 1);
let days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}
//获取昨天的日期
public getYesterDay(thisTime){
var time = new Date(thisTime); // 1 Feb -> 30 Jan
time.setDate(time.getDate() - 1);
let yesterDay = time.getFullYear()+"-" + (time.getMonth()+1) + "-" + time.getDate();
return yesterDay;
}
//获取明天的日期
public getTomorrow(thisTime){
var time = new Date(thisTime); // 1 Feb -> 30 Jan
time.setDate(time.getDate() +1);
let mymonth = time.getMonth()+1;
let myweekday = time.getDate();
if(mymonth < 10){
mymonth = <any>"0" + mymonth;
}
if(myweekday < 10){
myweekday = <any>"0" + myweekday;
}
let Tomorrow = time.getFullYear()+"-" + mymonth + "-" + myweekday;
return Tomorrow;
}

//获得本季度的开始月份
public getQuarterStartMonth(){
let quarterStartMonth = 0;
if(this.nowMonth<3){
quarterStartMonth = 0;
}
if(2<this.nowMonth && this.nowMonth<6){
quarterStartMonth = 3;
}
if(5<this.nowMonth && this.nowMonth<9){
quarterStartMonth = 6;
}
if(this.nowMonth>8){
quarterStartMonth = 9;
}
return quarterStartMonth;
}

//获取七天前的日期
public getSevenDaysDate(index){
//index= -7;index= 7 前后
let date = new Date(); //当前日期
let newDate = new Date();
newDate.setDate(date.getDate() + index);//官方文档上虽然说setDate参数是1-31,其实是可以设置负数的
let mymonth = newDate.getMonth()+1;
let myweekday = newDate.getDate();
if(mymonth < 10){
mymonth = <any>"0" + mymonth;
}
if(myweekday < 10){
myweekday = <any>"0" + myweekday;
}
let time = newDate.getFullYear()+"-"+mymonth+"-"+myweekday;
return time;
}



//获得本周的开始日期
public getWeekStartDate() {
this.addTiem();
let weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek);
return this.formatDate(weekStartDate);
}

//获得本周的结束日期
public getWeekEndDate() {
this.addTiem();
let weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek));
return this.formatDate(weekEndDate);
}
//获得上周的开始日期
public getLastWeekStartDate() {
let weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek - 7);
return this.formatDate(weekStartDate);
}
//获得上周的结束日期
public getLastWeekEndDate() {
let weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek - 1);
return this.formatDate(weekEndDate);
}

//获得本月的开始日期
public getMonthStartDate(){
this.addTiem();
let monthStartDate = new Date(this.nowYear, this.nowMonth, 1);
return this.formatDate(monthStartDate);
}

//获得本月的结束日期
public getMonthEndDate(){
this.addTiem();
let monthEndDate = new Date(this.nowYear, this.nowMonth, this.getMonthDays(this.nowMonth));
return this.formatDate(monthEndDate);
}

//获得上月开始时间
public getLastMonthStartDate(){
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
let lastYear = lastMonthDate.getFullYear();
let lastMonth = lastMonthDate.getMonth();

let lastMonthStartDate = new Date(this.nowYear, lastMonth, 1);
return this.formatDate(lastMonthStartDate);
}

//获得上月结束时间
public getLastMonthEndDate(){
this.addTiem();
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
let lastYear = lastMonthDate.getFullYear();
let lastMonth = lastMonthDate.getMonth();

let lastMonthEndDate = new Date(this.nowYear, lastMonth, this.getMonthDays(lastMonth));
return this.formatDate(lastMonthEndDate);
}

//获得本季度的开始日期
public getQuarterStartDate(){
this.addTiem();
let quarterStartDate = new Date(this.nowYear, this.getQuarterStartMonth(), 1);
return this.formatDate(quarterStartDate);
}

//或的本季度的结束日期
public getQuarterEndDate(){
this.addTiem();
let quarterEndMonth = this.getQuarterStartMonth() + 2;
let quarterStartDate = new Date(this.nowYear, quarterEndMonth, this.getMonthDays(quarterEndMonth));
return this.formatDate(quarterStartDate);
}
}

 评论