time.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * 毫秒转换友好的显示格式
  3. * 输出格式:21小时前
  4. * @param {[type]} time [description]
  5. * @return {[type]} [description]
  6. */
  7. const dateStr = (date) => {
  8. //获取js 时间戳
  9. var time = new Date().getTime();
  10. //去掉 js 时间戳后三位,与php 时间戳保持一致
  11. time = parseInt((time - date) / 1000);
  12. //存储转换值
  13. var s;
  14. if (time < 60 * 10) {//十分钟内
  15. return '刚刚';
  16. } else if ((time < 60 * 60) && (time >= 60 * 10)) {
  17. //超过十分钟少于1小时
  18. s = Math.floor(time / 60);
  19. return s + "分钟前";
  20. } else if ((time < 60 * 60 * 24) && (time >= 60 * 60)) {
  21. //超过1小时少于24小时
  22. s = Math.floor(time / 60 / 60);
  23. return s + "小时前";
  24. } else if ((time < 60 * 60 * 24 * 3) && (time >= 60 * 60 * 24)) {
  25. //超过1天少于3天内
  26. s = Math.floor(time / 60 / 60 / 24);
  27. return s + "天前";
  28. } else {
  29. //超过3天
  30. var date = new Date(parseInt(date));
  31. return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
  32. }
  33. }
  34. const getNowFormatDate = () => {
  35. var date = new Date();
  36. var year = date.getFullYear();
  37. var month = date.getMonth() + 1;
  38. var strDate = date.getDate();
  39. if (month >= 1 && month <= 9) {
  40. month = '0' + month;
  41. }
  42. if (strDate >= 0 && strDate <= 9) {
  43. strDate = '0' + strDate;
  44. }
  45. var currentdate = year + '-' + month + '-' + strDate + ":" + date.getHours() + ":" + date.getMinutes();
  46. console.log("XXXXXX", currentdate);
  47. return currentdate;
  48. }
  49. export default {
  50. dateStr, getNowFormatDate
  51. }