-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.js
More file actions
168 lines (137 loc) · 3.47 KB
/
datetime.js
File metadata and controls
168 lines (137 loc) · 3.47 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
"use strict"
import { getLang } from '../lang'
export function clone (d) {
return new Date(d.getTime())
}
export function addDays (d, days) {
let newDate = clone(d)
newDate.setDate(d.getDate() + days)
return newDate
}
export function addMonths (d, months) {
let newDate = clone(d)
newDate.setMonth(d.getMonth() + months)
return newDate
}
export function getFirstDayOfMonth (d) {
return new Date(d.getFullYear(), d.getMonth(), 1)
}
export function getDaysInMonth (d) {
let resultDate = getFirstDayOfMonth(d)
resultDate.setMonth(resultDate.getMonth() + 1)
resultDate.setDate(resultDate.getDate() - 1)
return resultDate.getDate()
}
export function getFullMonth (d) {
let month = d.getMonth()
return getLang('datetime.fullMonth')[month]
}
export function getShortMonth (d) {
let month = d.getMonth()
return getLang('datetime.shortMonth')[month]
}
export function getDayOfWeek (d) {
let weekday = d.getDay()
return getLang('datetime.weekday')[weekday]
}
export function getWeekArray (d) {
let dayArray = []
let daysInMonth = getDaysInMonth(d)
let daysInWeek
let emptyDays
let firstDayOfWeek
let week
let weekArray = []
for (let i = 1; i <= daysInMonth; i++) {
dayArray.push(new Date(d.getFullYear(), d.getMonth(), i))
}
while (dayArray.length) {
firstDayOfWeek = dayArray[0].getDay()
daysInWeek = 7 - firstDayOfWeek
emptyDays = 7 - daysInWeek
week = dayArray.splice(0, daysInWeek)
for (let j = 0; j < emptyDays; j++) {
week.unshift(null)
}
weekArray.push(week)
}
return weekArray
}
export function isEqualDate (d1, d2) {
return d1 && d2 &&
(d1.getFullYear() === d2.getFullYear()) &&
(d1.getMonth() === d2.getMonth()) &&
(d1.getDate() === d2.getDate())
}
export function isEqual (d1, d2) {
if (!d1 || !d2 || !(d1 instanceof Date) || !(d2 instanceof Date)) {
return false
}
return d1.getTime() === d2.getTime()
}
export function monthDiff (d1, d2) {
let m
m = (d1.getFullYear() - d2.getFullYear()) * 12
m += d1.getMonth()
m -= d2.getMonth()
return m
}
export function format (date, fmt) {
if (!date) { return '' }
if (!(date instanceof Date)) {
date = new Date(date)
}
let o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}
export function getDatetime (d) {
return format(d, getLang('datetime.format.datetime'))
}
export function getDate (d) {
return format(d, getLang('datetime.format.date'))
}
export function getFullYear (d) {
return format(d, getLang('datetime.format.year'))
}
export function getTime (d) {
return format(d, getLang('datetime.format.time'))
}
// string, unixtimestamp convert to Date
export function convert (obj, def) {
if (def === undefined) {
def = new Date()
}
if (!obj) {
return def
}
if (obj instanceof Date) {
return obj
}
if (/^[-+]?[0-9]+$/.test(obj)) {
obj = parseInt(obj) * 1000
} else {
obj = obj.replace(/-/g, "/")
}
try {
obj = new Date(obj)
} catch (e) {
obj = def
}
return obj
}