js将13位时间戳转换成日期格式

左手的ㄟ右手 2022-12-06 15:49 661阅读 0赞
  1. function formatterTime(time, fmt='yyyy-MM-dd hh:mm:ss') {
  2. if(!time){
  3. return '';
  4. }
  5. if(typeof(time) == "object" || typeof(time) == "OBJECT") {
  6. var z = {
  7. M: time.getMonth() + 1,
  8. d: time.getDate(),
  9. h: time.getHours(),
  10. m: time.getMinutes(),
  11. s: time.getSeconds()
  12. };
  13. fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
  14. return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2);
  15. });
  16. return fmt.replace(/(y+)/g, function(v) {
  17. return time.getFullYear().toString().slice(-v.length);
  18. });
  19. }else if(typeof(time) == "number"){
  20. var TIME = new Date( time) ;
  21. var z = {
  22. M: TIME.getMonth() + 1,
  23. d: TIME.getDate(),
  24. h: TIME.getHours(),
  25. m: TIME.getMinutes(),
  26. s: TIME.getSeconds()
  27. };
  28. fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
  29. return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2);
  30. });
  31. return fmt.replace(/(y+)/g, function(v) {
  32. return TIME.getFullYear().toString().slice(-v.length);
  33. });
  34. }
  35. else return time;
  36. }

推荐一个npm包silly-datetime,可直接调用,更方便

发表评论

表情:
评论列表 (有 0 条评论,661人围观)

还没有评论,来说两句吧...

相关阅读