vue的时间格式化

前端学渣 2019-11-11 03:41:40
我写全局日期格式化的函数,但是不知道为什么就年月出来,日还有时分秒都没法转换


Date.prototype.Format = function (fmt) {
var o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
'S': this.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
for (var 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
}
}



这是引用

console.log(new Date().Format('yyyyMMdd') + '')


这是出来的结果

...全文
779 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
傻蛋丫 2019-11-11
  • 打赏
  • 举报
回复
for循环大括号问题 改法一:去掉for循环12行 15行{} 改法二:for循环大括号闭合在return上一行
前端学渣 2019-11-11
  • 打赏
  • 举报
回复
咦惹!!!!!好的,解决了,谢谢。!!因为之前是没有括号的,后来严瑾模式要才加,难怪错了
Hello World, 2019-11-11
  • 打赏
  • 举报
回复
不能在for里return,要移到for之后:
Date.prototype.Format = function (fmt) {
var o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
'S': this.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
for (var 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;
}
前端学渣 2019-11-11
  • 打赏
  • 举报
回复
来个人救救孩子吧
前端学渣 2019-11-11
  • 打赏
  • 举报
回复
来人啊啊啊啊
前端学渣 2019-11-11
  • 打赏
  • 举报
回复
咦惹,为什么上面的代码成那样了

Date.prototype.Format = function (fmt) {
var o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
'S': this.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
for (var 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
}
}

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧