vue自定义组件之自定义时间组件

1.在src目录下新建utils文件夹,用于存放相应的js文件
在这里插入图片描述
2.相应的js文件中的代码如下

datetime.js:

Date.prototype.toMidnight = function() {
   
this.setHours(0)
this.setMinutes(0)
this.setSeconds(0)
this.setMilliseconds(0)
return this
}

Date.prototype.daysAgo = function(days, midnight) {
   
days = days ? days - 0 : 0
const date = new Date(this.getTime() - days * 8.64E7)
return midnight ? date.toMidnight() : date
}

Date.prototype.monthBegin = function(offset) {
   
offset = offset ? offset - 0 : 0
const days = this.getDate() - 1 - offset
return this.daysAgo(days, true)
}

Date.prototype.quarterBegin = function() {
   
const month = this.getMonth() - this.getMonth() % 3
return new Date(this.getFullYear(), month, 1).toMidnight()
}

Date.prototype.yearBegin = function() {
   
return new Date(this.getFullYear(), 0, 1).toMidnight()
}

Date.prototype.strftime = function(format, local) {
   
if (!format) {
   
const str = new Date(this.getTime() + 2.88E7).toISOString()
return str.substr(0, 16).replace('T', ' ')
}
local = local && local.startsWith('zh') ? 'zh' : 'en'
const padZero = function(str, len) {
   
const pads = len - str.toString().length
return (pads && pads > 0 ? '0'.repeat(pads) : '') + str
}
format = format.replace('%F', '%Y-%m-%d')
format = format.replace(/%D|%x/, '%m/%d/%y')
format = format.replace(/%T|%X/, '%H:%M:%S')
format = format.replace('%R', '%H:%M')
format = format.replace('%r', '%H:%M:%S %p')
format = format.replace('%c', '%a %b %e %H:%M:%S %Y')
const _this = this
return format.replace(/%[A-Za-z%]/g, function(f) {
   
let ans = f
switch (f) {
   
case '%%':
ans = '%'
break

case '%Y':
case '%G':
ans = _this.getFullYear()
break

case '%y':
ans = _this.getFullYear() % 100
break

case '%C':
ans = _this.getFullYear() / 100
break

case '%m':
case '%n':
ans = _this.getMonth() + 1
break

case '%B':
local = local.startsWith('en') 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值