Created
March 29, 2019 02:29
-
-
Save xjinza/baa72cddfc453dbcb3bb48a365c70227 to your computer and use it in GitHub Desktop.
时间格式化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
formatDate (timestamp, fmt) { | |
let date = new Date(timestamp); | |
if (/(y+)/.test(fmt)) { | |
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); | |
} | |
let o = { | |
'M+': date.getMonth() + 1, | |
'd+': date.getDate(), | |
'h+': date.getHours(), | |
'm+': date.getMinutes(), | |
's+': date.getSeconds() | |
}; | |
for (let k in o) { | |
if (new RegExp(`(${k})`).test(fmt)) { | |
let str = o[k] + ''; | |
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str)); | |
} | |
} | |
return fmt; | |
}, | |
padLeftZero (str) { | |
return ('00' + str).substr(str.length); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment