将纪元时间转换为小时前,从 iPhone 中的当前日期开始分钟前
Posted
技术标签:
【中文标题】将纪元时间转换为小时前,从 iPhone 中的当前日期开始分钟前【英文标题】:convert epoch time to hours ago, min ago from current date in iphone 【发布时间】:2012-09-17 08:01:35 【问题描述】:我的纪元时间是 1347522689。现在我想显示这个时间是多少小时前,距当前日期几分钟前。我是这个新手,请给我看一些代码或任何建议。
谢谢
【问题讨论】:
如果你想得到年、月、日、小时、秒,那么我有一个很好的代码...... @Rajneesh071 是的,请告诉我你的代码。非常感谢你的帮助。 【参考方案1】: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]);
【讨论】:
嗨 Vakul,感谢您的回复,但我已经尝试过了,但我仍然没有像 2 小时前那样,2 分钟前从纪元开始。我认为我们需要将第一个最后一个纪元时间与当前纪元时间进行比较。 它将纪元时间转换为正常时间......我会发布准确的答案......请等待 @user1011291 - 立即查看我的答案。 嗨,Vakul 很棒的家伙,谢谢...:) 欢迎您.... 【参考方案2】:使用下面的代码它工作得很好。
- (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];
【讨论】:
嗨 Mani,感谢您的回复,但我需要显示以从当前日期进行比较,然后显示时间和分钟前。例如,我在两天前发布了一些东西,然后我想在 2 天前从纪元开始。我希望你能帮助我。【参考方案3】:即使是较旧的问题日期,我也将这个函数放在这里以将秒转换为类似的格式
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];
希望对你有帮助
【讨论】:
@CyberMew 抱歉,这只是 nil 检查,例如 '(!fromDate ? YES : NO)',我会更新答案,谢谢以上是关于将纪元时间转换为小时前,从 iPhone 中的当前日期开始分钟前的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SQL Server 中将纪元时间与 24 小时前进行比较?
如何将(round)纪元时间转换为最接近的小时和日期wrt IST(在Java中)