time.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. function TimeFormatting(dateTimeStamp) {
  2. var result;
  3. var minute = 1000 * 60;
  4. var hour = minute * 60;
  5. var day = hour * 24;
  6. var halfamonth = day * 15;
  7. var month = day * 30;
  8. var now = new Date(); //当前时间
  9. // console.log('now', now);
  10. var digit = String(dateTimeStamp).length;
  11. if (digit == '10') {
  12. var curTime = new Date(parseInt(dateTimeStamp) * 1000); //后端返回的是秒数 过去时间
  13. } else {
  14. var curTime = new Date(parseInt(dateTimeStamp)); //后端返回的是秒数 过去时间
  15. }
  16. // console.log('curTime',curTime);
  17. var _year = now.getFullYear(); // 年
  18. var _month = now.getMonth() + 1; //月
  19. var _day = now.getDate(); //日
  20. var shitime = _year + '-' + _month + '-' + _day + ' 23:59:59';
  21. // console.log(shitime);
  22. var time_start = new Date(shitime.replace(/-/g, '/'));
  23. time_start.getTime();
  24. var diffValue = time_start.getTime() - curTime;
  25. // 过去时间和现在时间对比 相差多少天
  26. var CutOffDays = parseInt(diffValue / day);
  27. var Now_hour = now.getHours(); //当前 小时数
  28. var Now_mm = now.getMinutes(); //当前 分
  29. var Current_hour = curTime.getHours(); //过去 小时数
  30. var Current_mm = curTime.getMinutes(); //过去 分
  31. var Differhour = Now_hour - Current_hour //过去时间和现在时间相差小时
  32. var DifferMinute = Now_mm - Current_mm //过去时间和现在时间相差分钟
  33. // console.log(Differhour)
  34. // console.log(DifferMinute)
  35. var timestr = '';
  36. if (Current_hour < 11) {
  37. timestr = " 早上 ";
  38. }
  39. else if (Current_hour < 13) {
  40. timestr = " 中午 ";
  41. }
  42. else if (Current_hour < 17) {
  43. timestr = " 下午 ";
  44. }
  45. else if (Current_hour < 19) {
  46. timestr = " 晚上 ";
  47. }
  48. else {
  49. timestr = " 晚上 ";
  50. }
  51. // 日
  52. var currentTimeDay = curTime.getDate();
  53. //历史记录年月日
  54. var SpecificDate = curTime.getFullYear() + '年' + (curTime.getMonth() + 1) + '月' + currentTimeDay + '日 ';
  55. // console.log(SpecificDate);
  56. //历史记录时间
  57. var Historicaltime = curTime.getHours() + ':' + (curTime.getMinutes() >= 10 ? curTime.getMinutes() : ('0' + curTime.getMinutes()));
  58. // console.log(Historicaltime);
  59. var weekTime = (new Date(curTime)).getDay(); //星期 工作日
  60. // console.log(weekTime);
  61. var week = "";
  62. // console.log(CutOffDays, '2222');
  63. if (CutOffDays > 7) { // 消息超过 7天 显示日期
  64. result = SpecificDate + timestr + Historicaltime;
  65. } else if (CutOffDays < 1) { // 消息小于 1天
  66. result = timestr + Historicaltime;
  67. } else if (CutOffDays >= 1 && CutOffDays < 2) { // 消息大于 1天 并且 小于 2天 显示昨天
  68. result = '昨天' + timestr + Historicaltime;
  69. } else if (CutOffDays >= 2 && CutOffDays <= 7) { // 消息大于 2天 并且 小于 7天 显示星期
  70. // console.log(weekTime);
  71. switch (weekTime) {
  72. case 0:
  73. week = '星期日'
  74. break;
  75. case 1:
  76. week = '星期一'
  77. break;
  78. case 2:
  79. week = '星期二'
  80. break;
  81. case 3:
  82. week = '星期三'
  83. break;
  84. case 4:
  85. week = '星期四'
  86. break;
  87. case 5:
  88. week = '星期五'
  89. break;
  90. case 6:
  91. week = '星期六'
  92. break;
  93. }
  94. result = week + timestr + Historicaltime;
  95. }
  96. // console.log(result);
  97. return result;
  98. }
  99. //最下面一定要加上你自定义的方法(作用:将模块接口暴露出来),否则会报错:util.trim is not a function;
  100. module.exports = {
  101. TimeFormatting: TimeFormatting,
  102. }