time-utils.js
9.22 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* 获取该周的所要显示的周和日期的对应数据,数据结构如下
* var weekDay = {week: '',day: ''}
* 参数:selectWeek 0为本周,数字代表前几周或者后几周,例如1是下一周
*/
function getWeekDayList(selectWeek, selectDay, customerDisabled) {
// 1.获取周一对应得时间
// 2.用循环七次添加周一到周日对应得周几和几号
var selectWeekTime =
getCurrentTimeStamp(selectDay) + selectWeek * 7 * 24 * 60 * 60 * 1000
var mondayTime =
selectWeekTime - (getWeekNumber(selectWeekTime) - 1) * 24 * 60 * 60 * 1000
// let testDate = new Date(mondayTime)
// console.log(selectWeekTime, 'selectWeekTime')
// console.log(getWeekNumber(selectWeekTime), getWeekNumber(selectWeekTime) - 1, 'getWeekNumber(selectWeekTime) - 1')
// console.log(getDataFormat(selectWeekTime), 'selectWeekTime')
// console.log(selectWeekTime, 'selectWeekTime')
// console.log(getDataFormat(mondayTime), 'mondayTime')
var timeBean = {
selectDay: 0,
yearMonth: '',
weekDayList: []
}
timeBean.yearMonth = getYearMonth(selectWeekTime)
console.log(timeBean.yearMonth, 'timeBean.yearMonth')
const yearLabel = getYearMonth(selectWeekTime, '/')
// 获取当前时间戳(以毫秒为单位)
const currentTimestamp = Date.now()
for (var i = 0; i < 7; i++) {
var weekDay = {
week: '',
day: '',
disabled: '',
date: ''
}
let dataStr = mondayTime + i * 24 * 60 * 60 * 1000
weekDay.week = getWeek(dataStr)
weekDay.day = getMyDay(dataStr)
weekDay.date = getDataFormat(dataStr)
weekDay.disabled = customerDisabled
? dataStr > currentTimestamp + 24 * 60 * 60 * 1000
: dataStr > currentTimestamp
timeBean.weekDayList.push(weekDay)
if (i == 0) {
timeBean.starDay = getMyDay(dataStr)
timeBean.starDayLabel = getYearMonth(dataStr, '/') + '/' + weekDay.day
timeBean.reportStartDate = getDataFormat(dataStr)
}
if (i == 6) {
timeBean.endDayLabel = getYearMonth(dataStr, '/') + '/' + weekDay.day
timeBean.reportEndDate = getDataFormat(dataStr)
// console.log(dataStr)
timeBean.isInCurrentWeek = isTimestampInCurrentWeek(
dataStr,
customerDisabled
)
}
}
timeBean.selectDay = getCurrenrWeek(selectDay)
timeBean.formatYearMonth = getFormatYearMonth(selectWeekTime)
return timeBean
}
//获取当前时间戳 --
function getCurrentTimeStamp(selectDay) {
var timestamp
if (selectDay) {
timestamp = new Date(selectDay).getTime()
} else {
timestamp = new Date().getTime()
}
return timestamp
}
//获取当前周几
function getCurrenrWeek(selectDay) {
let str
if (selectDay) {
str = '6012345'.charAt(new Date(selectDay).getDay())
} else {
str = '6012345'.charAt(new Date().getDay())
}
return str
}
//时间戳获得年月
function getYearMonth(res, unit = '-') {
var time = new Date(res)
var y = time.getFullYear()
var m = time.getMonth() + 1
// 将月份转换成两位数的字符串
// var formattedMonth = month < 10 ? '0' + month : '' + month;
return y + unit + m
}
//时间戳获得年月
function getFormatYearMonth(res) {
var time = new Date(res)
var y = time.getFullYear()
var m = time.getMonth() + 1
// 将月份转换成两位数的字符串
var formattedMonth = m < 10 ? '0' + m : '' + m
return y + '-' + formattedMonth
}
//时间戳转几号
function getMyDay(res) {
var time = new Date(res)
var d = time.getDate()
return d
}
//时间戳转周几
function getWeek(res) {
// var time = new Date(res)
// var y = time.getFullYear()
// var m = time.getMonth() + 1
// var d = time.getDate()
// return '日一二三四五六'.charAt(new Date(y + '-' + m + '-' + d).getDay())
var days = ['日', '一', '二', '三', '四', '五', '六']
var time = new Date(res)
var dayIndex = time.getDay()
return days[dayIndex]
}
//时间戳转周几 0123456 --
function getWeekNumber(res) {
// var days = [6, 0, 1, 2, 3, 4, 5,];
var days = [7, 1, 2, 3, 4, 5, 6]
var time = new Date(res)
var dayIndex = time.getDay()
return days[dayIndex]
// var time = new Date(res)
// var y = time.getFullYear()
// var m = time.getMonth() + 1
// var d = time.getDate()
// return '0123456'.charAt(new Date(y + '-' + m + '-' + d).getDay())
}
//时间戳转年月份 --
function getDataFormat(res) {
// const timestamp = 1675372800000; // 时间戳,单位为毫秒
const date = new Date(res)
const year = date.getFullYear().toString()
const month = ('0' + (date.getMonth() + 1)).slice(-2)
const day = ('0' + date.getDate()).slice(-2)
return `${year}-${month}-${day}`
}
// function isTimestampInCurrentWeek(timestamp) {
// const currentDate = new Date()
// const currentDay = currentDate.getDay() // 获取当前日期是星期几
// // 计算当前周的起始日期(周一)
// const currentWeekStart = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() - currentDay + 1)
// console.log(currentWeekStart, 'currentWeekStart');
// // 计算当前周的结束日期(周日)
// const currentWeekEnd = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + (7 - currentDay), 23, 59, 59, 999)
// console.log(currentWeekEnd, 'currentWeekEnd');
// const targetDate = new Date(timestamp) // 不再乘以1000
// return targetDate >= currentWeekStart && targetDate <= currentWeekEnd
// }
function isTimestampInCurrentWeek(timestamp, customerDisabled) {
const currentDate = new Date()
const currentDay = currentDate.getDay() // 获取当前日期是星期几
// 计算当前周的起始日期(周一)
const currentWeekStart = new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
currentDate.getDate() - currentDay + (currentDay === 0 ? -6 : 1)
)
// 计算当前周的结束日期(周日)
const currentWeekEnd = new Date(
currentWeekStart.getFullYear(),
currentWeekStart.getMonth(),
currentWeekStart.getDate() + 6,
23,
59,
59,
999
)
const targetDate = new Date(timestamp)
if (customerDisabled) {
let startOfTargetDate = new Date(
targetDate.getFullYear(),
targetDate.getMonth(),
targetDate.getDate()
)
let endOfTargetDate = new Date(
startOfTargetDate.getTime() + 24 * 60 * 60 * 1000 - 1
)
let tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
return tomorrow <= endOfTargetDate
}
return targetDate >= currentWeekStart && targetDate <= currentWeekEnd
}
function getWeeksDiff(startDate, endDate) {
// 设置一周的第一天为周一
const firstDayOfWeek = 1 // 0表示周日,1表示周一,以此类推
// 获取开始日期和结束日期的时间戳(单位:毫秒)
const startTimestamp = new Date(startDate).getTime()
const endTimestamp = new Date(endDate).getTime()
// 获取结束日期是一周的第几天
const endDateObj = new Date(endDate)
let dayOfWeek = endDateObj.getDay()
// 如果结束日期是一周的第一天(周一),则不需要调整
if (dayOfWeek === firstDayOfWeek) {
dayOfWeek = 0
}
// 计算结束日期距离一周第一天的天数差
const daysDiff = (dayOfWeek + 7 - firstDayOfWeek) % 7
// 计算结束日期距离一周第一天的周数差
const weeksDiff = Math.floor(
(endTimestamp + daysDiff * 24 * 3600 * 1000 - startTimestamp) /
(7 * 24 * 3600 * 1000)
)
return weeksDiff
}
function formatDateStr(date = new Date()) {
const target = date instanceof Date ? date : new Date(date)
const y = target.getFullYear()
const m = `${target.getMonth() + 1}`.padStart(2, '0')
const d = `${target.getDate()}`.padStart(2, '0')
return `${y}-${m}-${d}`
}
function getTodayDateStr() {
return formatDateStr(new Date())
}
function isTodayReport(date) {
return formatDateStr(date) === getTodayDateStr()
}
function isDateWithin7Days(date, limitDate) {
const oneDay = 24 * 60 * 60 * 1000
const currentDate = new Date()
const targetDate = new Date(date)
if (limitDate) {
const limit = new Date(limitDate)
return targetDate.getTime() >= limit.getTime()
}
const diffMilliseconds = targetDate - currentDate
const diffDays = Math.floor(diffMilliseconds / oneDay)
return diffDays >= -7
}
function isDateWithin60Days(date) {
const oneDay = 24 * 60 * 60 * 1000 // 一天的毫秒数
const currentDate = new Date() // 当前日期
const targetDate = new Date(date) // 给定的日期
// 计算给定日期与当前日期的时间差(以毫秒为单位)
const diffMilliseconds = targetDate - currentDate
// 计算时间差对应的天数
const diffDays = Math.floor(diffMilliseconds / oneDay)
return diffDays >= -60
}
// 示例用法
// const inputDate = '2024-02-15';
// const isWithin7Days = isDateWithin7Days(inputDate);
// console.log(isWithin7Days); // 输出:true
// // 示例用法
// const startDate = '2024-02-19'; // 开始日期
// const endDate = '2024-02-25'; // 结束日期
// const weeksDiff = getWeeksDiff(startDate, endDate);
// console.log("开始日期距离结束日期的周数差:", weeksDiff);
module.exports = {
getWeekDayList: getWeekDayList,
formatDateStr: formatDateStr,
getTodayDateStr: getTodayDateStr,
isTodayReport: isTodayReport,
isDateWithin7Days: isDateWithin7Days,
isDateWithin60Days: isDateWithin60Days
}