2

我的纪元时间是 1347522689。现在我想显示这个时间是多少小时前,从当前日期开始几分钟前。我是这个新手,请给我看一些代码或任何建议,不胜感激。

谢谢

4

3 回答 3

2
NSString *epochTime = @"1347522689";
NSTimeInterval epochInterval = [epochTime longLongValue];
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:epochInterval];
NSDateFormatter *dateFormatterEpoch = [[NSDateFormatter alloc] init];
[dateFormatterEpoch setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSString *epochDate = [dateFormatterEpoch stringFromDate:epochNSDate];

NSDate *currentDateNSDate = [NSDate date];
NSDateFormatter *dateFormatterCurrent = [[NSDateFormatter alloc] init];
[dateFormatterCurrent setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSString *currentDate = [dateFormatterCurrent stringFromDate:currentDateNSDate];


NSLog(@"epochDate = %@",epochDate);
NSLog(@"currentDate = %@", currentDate);

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:epochNSDate toDate:currentDateNSDate options:0];

NSInteger year = [components month];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger min = [components minute];
NSInteger sec = [components second];


NSLog(@"Year Difference = %@\n Month Difference = %@\n, Days Difference = %@\n, Hours Difference = %@\n ,Minutes Difference = %@\n second Difference = %@\n", [[NSNumber numberWithInteger:year] stringValue] ,[[NSNumber numberWithInteger:months] stringValue], [[NSNumber numberWithInteger:days] stringValue], [[NSNumber numberWithInteger:hours] stringValue], [[NSNumber numberWithInteger:min] stringValue],[[NSNumber numberWithInteger:sec] stringValue]);
于 2012-09-17T08:10:11.587 回答
0

使用下面的代码它工作得很好。

- (NSString *)epochTimeConvert:(NSString *)strSeconds
{
    NSTimeInterval interval = [strSeconds doubleValue];
    //interval -= 3600;
    NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    return [dateFormatter stringFromDate:online];
}

像这样打电话

[self epochTimeConvert: 1347522689];
于 2012-09-17T08:20:43.537 回答
0

即使较旧的问题日期,我在这里放置此函数以将秒转换为如下格式

2 天 9 小时前

-(NSString*)humanReadableElapsedTimeForSeconds:(long)seconds WithUnitLabels:(NSArray*)customNames AgoLabel:(NSString*)customAgoLabel
{
    NSArray* names = ( customNames && [customNames count] == 7 ? customNames : @[@"seconds", @"minutes", @"hours", @"days", @"weeks", @"months", @"years"] );
    NSUInteger values[7] = {1, 60, 3600, 24 * 3600, 7 * 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600};
    NSString* agoLbl = (customAgoLabel ? customAgoLabel : @"ago");

    if (seconds < 0)
        return [NSString stringWithFormat:@"0 %@",[names objectAtIndex:0]];

    NSString* hrElapsedTime = nil;

    int i = 0;
    for(i = (sizeof(values)/sizeof(values[0])) - 1; i > 0 && seconds < values[i]; i--);

    if(i == 0) {
        hrElapsedTime = [NSString stringWithFormat:@"%d %@ %@",(int)floorf(seconds / (float)values[i]),[names objectAtIndex:i], agoLbl];
    } else {
        unsigned long t1 = (unsigned long)floorf(seconds / (float)values[i]);
        unsigned long t2 = (unsigned long)floorf((seconds - t1 * values[i]) / (float)values[i-1]);
        if (t2 == 0)
            hrElapsedTime = [NSString stringWithFormat:@"%lu %@ %@", t1, [names objectAtIndex:i], agoLbl];
        else
            hrElapsedTime = [NSString stringWithFormat:@"%lu %@, %lu %@ %@", t1, [names objectAtIndex:i], t2, [names objectAtIndex:(i-1)], agoLbl];
    }

    return hrElapsedTime;
}

所以从一个日期开始:

-(NSString*)humanReadableElapsedTimeFromDate:(NSDate*)fromDate WithUnitLabels:(NSArray*)customNames AgoLabel:(NSString*)customAgoLabel
{
    if (!fromDate)
        return nil;

    NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:fromDate];
    return [self humanReadableElapsedTimeForSeconds:seconds WithUnitLabels:customNames AgoLabel:customAgoLabel];
}

用法:

//from seconds ( in this case will output '3 minutes, 20 seconds ago' )
[self humanReadableElapsedTimeForSeconds:200 WithUnitLabels:nil AgoLabel:nil];
// from date
[self humanReadableElapsedTimeFromDate:someDate WithUnitLabels:nil AgoLabel:nil];

希望它会有所帮助

于 2014-04-09T09:53:27.130 回答