In the project, we often meet some requirements, that is, how to turn the time format into a format we want , Of course , You can use some third-party plug-ins, such as moment, This is what others have already packaged , If you want to be a thoughtful programmer , Not a porter , You have to understand js How to encapsulate a time format function , The code is as follows :
function formatDate(value, fmt) { let getDate = new Date(value); let o = { 'M+'
: getDate.getMonth() + 1, 'd+': getDate.getDate(), 'h+': getDate.getHours(),
'm+': getDate.getMinutes(), 's+': getDate.getSeconds(), 'q+': Math.floor((
getDate.getMonth() + 3) / 3), 'S': getDate.getMilliseconds() }; if (/(y+)/.test(
fmt)) { fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 -
RegExp.$1.length)); } for (let 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; } let d = new Date(); console.
log(formatDate(d, 'yyyy-MM-dd')); // 2020-09-18 console.log(formatDate(d,
'yyyy:MM:dd')); // 2020:09:18 console.log(formatDate(d, 'yyyy-MM-dd hh:mm'));
// 2020-09-18 17:26

Technology
©2020 ioDraw All rights reserved
Swing actual combat Understanding closure about vue in el-date-picker type=daterange The problem of date not echoing Ubuntu 18.04 swap Partition expansion Snake code --c Language Edition visual c++6.0 open 【Golang Basic series 10 】Go language On conditional sentences ifString class —— summary ,String The nature of , Memory resolution , Commonly used APIAdobe Illustrator Publish native support Apple Silicon Test version of the software study java My first class antd table sort