Skip to content

Commit

Permalink
helper timesAge
Browse files Browse the repository at this point in the history
  • Loading branch information
uniquexiaobai committed Oct 10, 2016
1 parent 2f3f90d commit a3777eb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
38 changes: 37 additions & 1 deletion pages/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@ function formateActionType (type) {
}
}

function timesAge (date) {
const currentDate = new Date();
const ghDate = new Date(date);

return timeDifference(currentDate.getTime(), ghDate.getTime());
}

function timeDifference (current, previous) {
const msPerMinute = 60 * 1000;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 60;
const msPerWeek = msPerDay * 7;
const msPerMouth = msPerWeek * 4;
const msPerYear = msPerMouth * 12;

const elapsed = Math.abs(current - previous);

switch (true) {
case elapsed < msPerMinute:
return Math.round(elapsed / 1000) + ' seconds age';
case elapsed < msPerHour:
return Math.round(elapsed / msPerMinute) + ' minutes age';
case elapsed < msPerDay:
return Math.round(elapsed / msPerHour) + ' hours age';
case elapsed < msPerWeek:
return Math.round(elapsed / msPerDay) + ' days age';
case elapsed < msPerMouth:
return `approximately ${Math.round(elapsed / msPerWeek)} weeks age`;
case elapsed < msPerYear:
return `approximately ${Math.round(elapsed / msPerMouth)} mouths age`;
default:
return `approximately ${Math.round(elapsed / msPerYear)} years age`;
}
}

module.exports = {
formateActionType: formateActionType
formateActionType: formateActionType,
timesAge: timesAge
};
2 changes: 1 addition & 1 deletion pages/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Page({

fetchEventsData (url) {
services.fetch(url).then(res => {
console.log(res.data);
res.data.forEach(item => {
item.type = helper.formateActionType(item.type);
item.created_at = helper.timesAge(item.created_at);
});
this.setData({
items: this.data.items.concat(res.data),
Expand Down
2 changes: 1 addition & 1 deletion pages/index/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<image class="actor_avatar" mode="aspectFit" src="{{item.actor.avatar_url}}"></image>
<view class="col">
<view class="actor_name">{{item.actor.display_login}}</view>
<text class="action_time">1 hours ago</text>
<text class="action_time">{{item.created_at}}</text>
</view>
</view>
<view class="row received_event_bottom">
Expand Down

0 comments on commit a3777eb

Please sign in to comment.