将日期时间转换为指定格式,如:YYYY-mm-dd HH:MM表示2019-06-06 19:45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function dateFormat(fmt, date) { let ret; const opt = { "Y+": date.getFullYear().toString(), // 年 "m+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "M+": date.getMinutes().toString(), // 分 "S+": date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; } |
用法:
1 2 3 |
let date = new Date() dateFormat("YYYY-mm-dd HH:MM", date) >>> 2019-06-06 19:45` |
2020-11-23: 如果对日期时间处理的需求较多, 推荐moment.js这个日期处理类库, 简单方便. 作者:雷子_ 链接:https://www.jianshu.com/p/49fb78bca621 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View Details在watch中使用this要注意,不能用箭头函数,否则会出错,例如:
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 |
<template> </template> <script> export default{ data(){ return { isLogin: false } }, watch:{ isLogin: (newVal, oldVal) => { newVal && this.updateLogin() } }, methods:{ updateLogin(){ console.log(`update login`) } } } </script> <style scoped> </style> |
箭头函数要改为function的形式,如下:
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 |
<template> </template> <script> export default{ data(){ return { isLogin: false } }, watch:{ isLogin: function(newVal, oldVal) { newVal && this.updateLogin() } }, methods:{ updateLogin(){ console.log(`update login`) } } } </script> <style scoped> </style> |
from:https://blog.csdn.net/liubangbo/article/details/115242420
View Details