更改timeAgo到期系统

时间:2016-03-14 09:36:55

标签: ios swift nsdate

在我目前的项目中,我使用的扩展程序显示了多久以前发布的帖子。使用此扩展程序:

extension NSDate {
    var timeAgo: String {
        let minute = 60
        let hour = 60 * minute
        let day = 24 * hour
        let secondsAgo = Int(NSDate().timeIntervalSinceDate(self))
        if secondsAgo < 0            { return  "later"                           }
        if secondsAgo == 0           { return "now"                              }
        if secondsAgo == 1           { return "1 second ago"                     }
        if secondsAgo < minute       { return "\(secondsAgo) seconds ago"        }
        if secondsAgo < (2 * minute) { return "1 minute ago"                     }
        if secondsAgo < hour         { return "\(secondsAgo/minute) minutes ago" }
        if secondsAgo < 2 * hour     { return "1 hour ago"                       }
        if secondsAgo < day          { return "\(secondsAgo / hour) hours ago"   }
        let formatter = NSDateFormatter()
        formatter.dateFormat = "M/d/yy"
        return formatter.stringFromDate(self)
    }
}

这是我将上传的时间戳设置为帖子的方式:

imageCell?.uploadedTimeLabel.text = createdAt[indexPath.item].timeAgo

但我怎样才能建立过期系统呢?用户可以在24小时内设置到期,在帖子中,它会说“10小时内过期”,“8分钟后过期”等等。当到期时间达到0时,帖子会被删除。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我已经进行了部分编码,在代码注释中几乎没有提及列表。

NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"dd/MM/yyyy HH:mm a"];
    [format setTimeZone:[NSTimeZone systemTimeZone]];
    NSDictionary *dictPost1=[[NSDictionary alloc] initWithObjects:@[@"post one",@"14/03/2016 04:15 pm"] forKeys:@[@"postName",@"timeStapmp"]];

     NSDictionary *dictPost2=[[NSDictionary alloc] initWithObjects:@[@"post two",@"14/03/2016 03:15 pm"] forKeys:@[@"postName",@"timeStapmp"]];

    NSArray *arrposts=[[NSArray alloc] initWithObjects:dictPost1,dictPost2, nil];
    for (NSDictionary *dictPost in arrposts) {
        NSDate *postedDate=[format dateFromString:[dictPost objectForKey:@"timeStapmp"]];
        NSLog(@"difference=%f in miliseconds",[postedDate timeIntervalSinceNow]);
        //To-do 1.convert millisecond difference into hour and mintue
        //To-do 2. subtract converted hour from 10, reaming time to expire
    }