detalTime.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * 创建时间和现在时间作对比
  3. * @param time 时间戳
  4. *
  5. * */
  6. export function beforeTime(time) {
  7. let way = Date.parse(new Date()) / 1000 - time;
  8. let r = '';
  9. if (way < 60) {
  10. r = '刚刚';
  11. } else if (way >= 60 && way < 3600) {
  12. r = Math.floor(way / 60) + '分钟前';
  13. } else if (way >= 3600 && way < 86400) {
  14. r = Math.floor(way / 3600) + '小时前';
  15. } else if (way >= 86400 && way < 2592000) {
  16. r = Math.floor(way / 86400) + '天前';
  17. } else if (way >= 2592000 && way < 15552000) {
  18. r = Math.floor(way / 2592000) + '个月前';
  19. } else if (time === 0 || !time) {
  20. r = '暂无';
  21. } else {
  22. r = dateChange('Y-m-d', time);
  23. }
  24. return r;
  25. }
  26. /*
  27. * 返回时间年月日时分秒
  28. * @param {date} 时间戳
  29. * @param {fmt} 转换格式 'yyyy-MM-dd hh:mm:ss'
  30. *
  31. * */
  32. export function formatDate(date, fmt) {
  33. if (/(y+)/.test(fmt)) {
  34. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  35. }
  36. var o = {
  37. 'M+': date.getMonth() + 1,
  38. 'd+': date.getDate(),
  39. 'h+': date.getHours(),
  40. 'm+': date.getMinutes(),
  41. 's+': date.getSeconds()
  42. };
  43. for (let k in o) {
  44. if (new RegExp(`(${k})`).test(fmt)) {
  45. let str = o[k] + '';
  46. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
  47. }
  48. }
  49. return fmt;
  50. };
  51. function padLeftZero(str) {
  52. return ('00' + str).substr(str.length);
  53. }