Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```js
- formatTime(date, format = 'yyyy-MM-dd hh:mm:ss') {
- if (date instanceof Date) {
- const syntaxes = {
- '(y+)': date.getFullYear(),
- '(M+)': date.getMonth() + 1,
- '(d+)': date.getDate(),
- '(h+)': date.getHours(),
- '(m+)': date.getMinutes(),
- '(s+)': date.getSeconds(),
- }
- for (const syntax in syntaxes) {
- const result = format.match(syntax)
- if (result !== null) {
- format = format.replace(result[0], `000${syntaxes[syntax]}`.substr(-(result[0].length)))
- }
- }
- return format
- } else {
- return ''
- }
- }
- ```
Add Comment
Please, Sign In to add comment