我有一个纪元时间是1347522689.现在我想在几分钟前,从当前日期前几分钟前显示这个时间。我是新来的,请告诉我一些代码或任何建议。
由于
答案 0 :(得分: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]);
答案 1 :(得分: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];
答案 2 :(得分: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];
希望它有所帮助