当前时间
export const nowTime = () => {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const date = now.getDate() >= 10 ? now.getDate() : ('0' + now.getDate());
const hour = now.getHours() >= 10 ? now.getHours() : ('0' + now.getHours());
const miu = now.getMinutes() >= 10 ? now.getMinutes() : ('0' + now.getMinutes());
const sec = now.getSeconds() >= 10 ? now.getSeconds() : ('0' + now.getSeconds());
return +year + "年" + (month + 1) + "月" + date + "日 " + hour + ":" + miu + ":" + sec;
}
格式化时间
export const dateFormater = (formater, time) => {
let date = time ? new Date(time) : new Date(),
Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
return formater.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g,(M<10 ? '0' : '') + M)
.replace(/DD/g,(D<10 ? '0' : '') + D)
.replace(/HH|hh/g,(H<10 ? '0' : '') + H)
.replace(/mm/g,(m<10 ? '0' : '') + m)
.replace(/ss/g,(s<10 ? '0' : '') + s)
}
// dateFormater('YYYY-MM-DD HH:mm:ss')
// dateFormater('YYYYMMDDHHmmss')
以上是编程学习网小编为您介绍的“JavaScript开发小技巧之各种时间操作”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:JavaScript开发小技巧之各种时间操作
猜你喜欢
- 正则表达式练习器 2024-01-16
- vue3.0实现移动端自适应 2023-10-08
- 突袭HTML5之Javascript API扩展3—本地存储全新体验 2024-02-29
- 浅谈JavaScript的对象类型之function 2023-07-10
- 一个网站部署多个Google Analytics帐户 2024-02-29
- js判断是否按下了Shift键的方法 2023-12-26
- JavaScript开发小技巧之数组技巧 2024-12-08
- jQuery实现选中弹出窗口选择框内容后赋值给文本框的方法 2023-12-24
- html5中的图片预览 2023-10-27
- CSS网页布局:div水平居中的各种方法 2023-12-14