开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
element3
/
src
/
utils
/
date-util.js
element3
/
src
/
utils
/
date-util.js
date-util.js 8.84 KB
一键复制 编辑 原始数据 按行查看 历史
woniu 提交于 2020年07月24日 17:11 +08:00 . chore:update eslint semi
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
import fecha from 'element-ui/src/utils/date'
import { t } from 'element-ui/src/locale'
const weeks = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
const months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
const newArray = function(start, end) {
let result = []
for (let i = start; i <= end; i++) {
result.push(i)
}
return result
}
export const getI18nSettings = () => {
return {
dayNamesShort: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
dayNames: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
monthNamesShort: months.map(month => t(`el.datepicker.months.${ month }`)),
monthNames: months.map((month, index) => t(`el.datepicker.month${ index + 1 }`)),
amPm: ['am', 'pm']
}
}
export const toDate = function(date) {
return isDate(date) ? new Date(date) : null
}
export const isDate = function(date) {
if (date === null || date === undefined) return false
if (isNaN(new Date(date).getTime())) return false
if (Array.isArray(date)) return false // deal with `new Date([ new Date() ]) -> new Date()`
return true
}
export const isDateObject = function(val) {
return val instanceof Date
}
export const formatDate = function(date, format) {
date = toDate(date)
if (!date) return ''
return fecha.format(date, format || 'yyyy-MM-dd', getI18nSettings())
}
export const parseDate = function(string, format) {
return fecha.parse(string, format || 'yyyy-MM-dd', getI18nSettings())
}
export const getDayCountOfMonth = function(year, month) {
if (month === 3 || month === 5 || month === 8 || month === 10) {
return 30
}
if (month === 1) {
if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
return 29
} else {
return 28
}
}
return 31
}
export const getDayCountOfYear = function(year) {
const isLeapYear = year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)
return isLeapYear ? 366 : 365
}
export const getFirstDayOfMonth = function(date) {
const temp = new Date(date.getTime())
temp.setDate(1)
return temp.getDay()
}
// see: https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript
// {prev, next} Date should work for Daylight Saving Time
// Adding 24 * 60 * 60 * 1000 does not work in the above scenario
export const prevDate = function(date, amount = 1) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() - amount)
}
export const nextDate = function(date, amount = 1) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount)
}
export const getStartDateOfMonth = function(year, month) {
const result = new Date(year, month, 1)
const day = result.getDay()
if (day === 0) {
return prevDate(result, 7)
} else {
return prevDate(result, day)
}
}
export const getWeekNumber = function(src) {
if (!isDate(src)) return null
const date = new Date(src.getTime())
date.setHours(0, 0, 0, 0)
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7)
// January 4 is always in week 1.
const week1 = new Date(date.getFullYear(), 0, 4)
// Adjust to Thursday in week 1 and count number of weeks from date to week 1.
// Rounding should be fine for Daylight Saving Time. Its shift should never be more than 12 hours.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7)
}
export const getRangeHours = function(ranges) {
const hours = []
let disabledHours = [];
(ranges || []).forEach(range => {
const value = range.map(date => date.getHours())
disabledHours = disabledHours.concat(newArray(value[0], value[1]))
})
if (disabledHours.length) {
for (let i = 0; i < 24; i++) {
hours[i] = disabledHours.indexOf(i) === -1
}
} else {
for (let i = 0; i < 24; i++) {
hours[i] = false
}
}
return hours
}
export const getPrevMonthLastDays = (date, amount) => {
if (amount <= 0) return []
const temp = new Date(date.getTime())
temp.setDate(0)
const lastDay = temp.getDate()
return range(amount).map((_, index) => lastDay - (amount - index - 1))
}
export const getMonthDays = (date) => {
const temp = new Date(date.getFullYear(), date.getMonth() + 1, 0)
const days = temp.getDate()
return range(days).map((_, index) => index + 1)
}
function setRangeData(arr, start, end, value) {
for (let i = start; i < end; i++) {
arr[i] = value
}
}
export const getRangeMinutes = function(ranges, hour) {
const minutes = new Array(60)
if (ranges.length > 0) {
ranges.forEach(range => {
const start = range[0]
const end = range[1]
const startHour = start.getHours()
const startMinute = start.getMinutes()
const endHour = end.getHours()
const endMinute = end.getMinutes()
if (startHour === hour && endHour !== hour) {
setRangeData(minutes, startMinute, 60, true)
} else if (startHour === hour && endHour === hour) {
setRangeData(minutes, startMinute, endMinute + 1, true)
} else if (startHour !== hour && endHour === hour) {
setRangeData(minutes, 0, endMinute + 1, true)
} else if (startHour < hour && endHour > hour) {
setRangeData(minutes, 0, 60, true)
}
})
} else {
setRangeData(minutes, 0, 60, true)
}
return minutes
}
export const range = function(n) {
// see https://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n
return Array.apply(null, {length: n}).map((_, n) => n)
}
export const modifyDate = function(date, y, m, d) {
return new Date(y, m, d, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())
}
export const modifyTime = function(date, h, m, s) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds())
}
export const modifyWithTimeString = (date, time) => {
if (date == null || !time) {
return date
}
time = parseDate(time, 'HH:mm:ss')
return modifyTime(date, time.getHours(), time.getMinutes(), time.getSeconds())
}
export const clearTime = function(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
}
export const clearMilliseconds = function(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0)
}
export const limitTimeRange = function(date, ranges, format = 'HH:mm:ss') {
// TODO: refactory a more elegant solution
if (ranges.length === 0) return date
const normalizeDate = date => fecha.parse(fecha.format(date, format), format)
const ndate = normalizeDate(date)
const nranges = ranges.map(range => range.map(normalizeDate))
if (nranges.some(nrange => ndate >= nrange[0] && ndate <= nrange[1])) return date
let minDate = nranges[0][0]
let maxDate = nranges[0][0]
nranges.forEach(nrange => {
minDate = new Date(Math.min(nrange[0], minDate))
maxDate = new Date(Math.max(nrange[1], minDate))
})
const ret = ndate < minDate ? minDate : maxDate
// preserve Year/Month/Date
return modifyDate(
ret,
date.getFullYear(),
date.getMonth(),
date.getDate()
)
}
export const timeWithinRange = function(date, selectableRange, format) {
const limitedDate = limitTimeRange(date, selectableRange, format)
return limitedDate.getTime() === date.getTime()
}
export const changeYearMonthAndClampDate = function(date, year, month) {
// clamp date to the number of days in `year`, `month`
// eg: (2010年1月31日, 2010, 2) => 2010年2月28日
const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month))
return modifyDate(date, year, month, monthDate)
}
export const prevMonth = function(date) {
const year = date.getFullYear()
const month = date.getMonth()
return month === 0
? changeYearMonthAndClampDate(date, year - 1, 11)
: changeYearMonthAndClampDate(date, year, month - 1)
}
export const nextMonth = function(date) {
const year = date.getFullYear()
const month = date.getMonth()
return month === 11
? changeYearMonthAndClampDate(date, year + 1, 0)
: changeYearMonthAndClampDate(date, year, month + 1)
}
export const prevYear = function(date, amount = 1) {
const year = date.getFullYear()
const month = date.getMonth()
return changeYearMonthAndClampDate(date, year - amount, month)
}
export const nextYear = function(date, amount = 1) {
const year = date.getFullYear()
const month = date.getMonth()
return changeYearMonthAndClampDate(date, year + amount, month)
}
export const extractDateFormat = function(format) {
return format
.replace(/\W?m{1,2}|\W?ZZ/g, '')
.replace(/\W?h{1,2}|\W?s{1,3}|\W?a/gi, '')
.trim()
}
export const extractTimeFormat = function(format) {
return format
.replace(/\W?D{1,2}|\W?Do|\W?d{1,4}|\W?M{1,4}|\W?y{2,4}/g, '')
.trim()
}
export const validateRangeInOneMonth = function(start, end) {
return (start.getMonth() === end.getMonth()) && (start.getFullYear() === end.getFullYear())
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/57code/element3.git
git@gitee.com:57code/element3.git
57code
element3
element3
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /