Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
fix(filters): 服务器返回的值为祖鲁时间,需要进行时间差转化
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Jul 25, 2018
1 parent ac9f37b commit 5df49ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ export const time2txt = str => {
}
};

// 格林威治时间和本地时间之间的时差 (单位:毫秒)
/**
* 祖鲁时间和本地时间之间的时差 (单位:毫秒)
* @returns {number} timezone offset
*/
export const timeOffset = new Date().getTimezoneOffset() * 60 * 1000;

export const time2tips = date => {
const time =
new Date(typeof date === "string" ? date.replace(/-/g, "/") : date) -
timeOffset;
const offset = (new Date().getTime() - time) / 1000;
const time = new Date(date);
// 服务器返回的时间是祖鲁时间,需要进行本地化
const offset = (new Date().getTime() - time + timeOffset) / 1000;
if (offset < 60) return "1分钟内";
if (offset < 3600) return `${~~(offset / 60)}分钟前`;
if (offset < 3600 * 24) return `${~~(offset / 3600)}小时前`;
Expand Down
8 changes: 5 additions & 3 deletions src/page/wallet/WalletDetailItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
</template>

<script>
import { timeOffset } from "@/filters";
const week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
function splitYMD(date) {
date = date || new Date();
date = new Date(date);
const Y = date.getFullYear();
const M = date.getMonth() + 1;
const D = date.getDate();
Expand Down Expand Up @@ -43,9 +46,8 @@ export default {
computed: {
created_at() {
const now = splitYMD(new Date());
const time = splitYMD(
new Date(this.detail.created_at.replace(/-/g, "/"))
);
let time = new Date(this.detail.created_at).getTime() - timeOffset;
time = splitYMD(time);
let D;
if (time.Y < now.Y) {
D = time.d;
Expand Down

0 comments on commit 5df49ce

Please sign in to comment.