Commit 49d4a4c7979792d802b05d0f6ce599d64f7f2d8a

Authored by zhiyangdu
1 parent a71ce81f

提交

src/components/darenListCom/darenListCom.vue
@@ -11,7 +11,10 @@ @@ -11,7 +11,10 @@
11 </div> 11 </div>
12 <div class="top-right"> 12 <div class="top-right">
13 <!-- 发布时间 --> 13 <!-- 发布时间 -->
14 - {{item.time}} 14 + <div class="day" v-if="getDatetime(item.addtime, item.time).day">
  15 + {{getDatetime(item.addtime, item.time).day}}
  16 + </div>
  17 + <div class="date">{{getDatetime(item.addtime, item.time).date}}</div>
15 </div> 18 </div>
16 </div> 19 </div>
17 20
@@ -97,6 +100,92 @@ @@ -97,6 +100,92 @@
97 } 100 }
98 }, 101 },
99 props: ["item"], 102 props: ["item"],
  103 + computed: {
  104 +
  105 + // 计算显示日期
  106 + getDatetime() {
  107 +
  108 + function isLeapYear(year) {
  109 + if (year % 4 == 0&&year % 100 != 0||year % 400 ==0) {
  110 + return true;
  111 + } else {
  112 + return false;
  113 + }
  114 + }
  115 +
  116 + return function(addTime, nowTime, index = undefined) {
  117 +
  118 + // 返回数据
  119 + let result = {
  120 + year: "",
  121 + day: "",
  122 + date: ""
  123 + };
  124 +
  125 + // 设置时间对象
  126 + let now = new Date(nowTime * 1000);
  127 + let add = new Date(addTime * 1000);
  128 +
  129 + // 一年中的第几天
  130 + let nowDays = Math.ceil(( now - new Date(now.getFullYear().toString())) / (24 * 60 * 60 * 1000)) + 1;
  131 + let addDays = Math.ceil(( add - new Date(add.getFullYear().toString())) / (24 * 60 * 60 * 1000)) + 1;
  132 +
  133 + // 根据情况,判断今天/昨天/前天
  134 + if (nowDays === addDays && now.getFullYear() === add.getFullYear()) { // 今天
  135 + result.year = "";
  136 + result.day = "";
  137 + result.date = (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  138 + } else if (nowDays > addDays && now.getFullYear() === add.getFullYear()) { // 正常大于,判断大于多少,设置昨天/前天/日期
  139 + result.year = "";
  140 + if (nowDays - addDays === 1) {
  141 + result.day = "昨天";
  142 + result.date = (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  143 + } else if (nowDays - addDays === 2) {
  144 + result.day = "前天";
  145 + result.date = (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  146 + } else {
  147 + result.day = "";
  148 + result.date = (add.getMonth() + 1) + "-" + add.getDate() + " " + (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  149 + }
  150 + } else if (now.getFullYear() > add.getFullYear()) { // 跨年,判断设置年份和闰年等,设置昨天/前天/日期
  151 +
  152 + // 设置年
  153 + if (!this.years.includes(add.getFullYear())) {
  154 + result.year = add.getFullYear();
  155 + this.years.push(add.getFullYear());
  156 + } else {
  157 + result.year = "";
  158 + }
  159 +
  160 + // 一年的天数
  161 + let fullYearDay = 365;
  162 +
  163 + // 判断是否为闰年
  164 + if (isLeapYear(add.getFullYear())) {
  165 + fullYearDay = 366;
  166 + }
  167 +
  168 + // 判断是夸一年还是多年
  169 + if (now.getFullYear() - add.getFullYear() === 1) {
  170 +
  171 + // 设置昨天/前天/日期
  172 + if (nowDays + fullYearDay - addDays === 1) {
  173 + result.day = "昨天";
  174 + result.date = (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  175 + } else if (nowDays + fullYearDay - addDays === 2) {
  176 + result.day = "前天";
  177 + result.date = (add.getHours() >= 10 ? add.getHours() : "0" + add.getHours()) + ":" + (add.getMinutes() >= 10 ? add.getMinutes() : "0" + add.getMinutes());
  178 + }
  179 + } else {
  180 + result.day = "";
  181 + result.date = (add.getMonth() + 1) + "-" + add.getDate();
  182 + }
  183 + }
  184 +
  185 + return result;
  186 + }
  187 + }
  188 + },
100 components: { 189 components: {
101 userAvatar, 190 userAvatar,
102 palaceGrid, 191 palaceGrid,
@@ -135,7 +224,7 @@ @@ -135,7 +224,7 @@
135 */ 224 */
136 goodsPrimary(item) { 225 goodsPrimary(item) {
137 this.$emit("goodsPrimary", item); 226 this.$emit("goodsPrimary", item);
138 - } 227 + },
139 } 228 }
140 } 229 }
141 </script> 230 </script>