Vue格式化日期时间为(YYYY-MM-DD hh:mm:ss)
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
·
- 在src下创建一个文件plugins的文件,如图

- 在里面的format.js文件下写入
function format (date, format) {
if (!date) {
return ''
}
let d = new Date(date)
// 年
if (/YYYY/.test(format)) {
format = format.replace(/YYYY/, d.getFullYear())
}
// 月份
let month = d.getMonth() + 1
if (/MM/.test(format)) {
let monthStr = month < 10 ? '0' + month : month
format = format.replace(/MM/, monthStr)
} else if (/M/.test(format)) {
format = format.replace(/M/, month)
}
// 日期
let dates = d.getDate()
if (/DD/.test(format)) {
let dateStr = dates < 10 ? '0' + dates : dates
format = format.replace(/DD/, dateStr)
} else if (/D/.test(format)) {
format = format.replace(/D/, dates)
}
// 小时
let hours = d.getHours()
if (/HH/.test(format)) {
let hoursStr = hours < 10 ? '0' + hours : hours
format = format.replace(/HH/, hoursStr)
} else if (/H/.test(format)) {
format = format.replace(/H/, hours)
} else if (/hh/.test(format)) {
let hoursMin = hours > 12 ? hours - 12 : hours
let hoursStr = hoursMin < 10 ? '0' + hoursMin : hoursMin
format = format.replace(/hh/, hoursStr)
} else if (/h/.test(format)) {
let hoursMin = hours > 12 ? hours - 12 : hours
format = format.replace(/h/, hoursMin)
}
// 分
let minutes = d.getMinutes()
if (/mm/.test(format)) {
let minutesStr = minutes < 10 ? '0' + minutes : minutes
format = format.replace(/mm/, minutesStr)
} else if (/m/.test(format)) {
format = format.replace(/m/, minutes)
}
// 秒
let seconds = d.getSeconds()
if (/ss/.test(format)) {
let secondsStr = seconds < 10 ? '0' + seconds : seconds
format = format.replace(/ss/, secondsStr)
} else if (/s/.test(format)) {
format = format.replace(/s/, seconds)
}
return format
}
export default format
- 在所需的组件页面下引入
import format from '@/plugins/format.js'
- 将startLimit参数格式化
this.startLimit = format( this.startLimit, 'YYYY-MM-DD HH:mm:ss')
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:3 个月前 )
9e887079
[skip ci] 1 年前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)