Guest User

Untitled

a guest
Jul 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. ```js
  2. formatTime(date, format = 'yyyy-MM-dd hh:mm:ss') {
  3. if (date instanceof Date) {
  4. const syntaxes = {
  5. '(y+)': date.getFullYear(),
  6. '(M+)': date.getMonth() + 1,
  7. '(d+)': date.getDate(),
  8. '(h+)': date.getHours(),
  9. '(m+)': date.getMinutes(),
  10. '(s+)': date.getSeconds(),
  11. }
  12. for (const syntax in syntaxes) {
  13. const result = format.match(syntax)
  14. if (result !== null) {
  15. format = format.replace(result[0], `000${syntaxes[syntax]}`.substr(-(result[0].length)))
  16. }
  17. }
  18. return format
  19. } else {
  20. return ''
  21. }
  22. }
  23. ```
Add Comment
Please, Sign In to add comment