Created
March 11, 2013 05:21
-
-
Save helloworld116/5132052 to your computer and use it in GitHub Desktop.
oc:transform:日期时间相关
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//返回YYYY-MM-DD HH:MM:SS格式的当前时间 | |
+(NSString *)getCurrentTime{ | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; | |
return [dateFormatter stringFromDate:[NSDate date]]; | |
} | |
//如果信息发表在1小时之前显示分钟,一天之内的信息显示小时,10天之内按天显示,10天之外按所发表时间进行显示: | |
+ (NSString *)intervalSinceNow: (NSString *) theDate | |
{ | |
NSDateFormatter *date=[[NSDateFormatter alloc] init]; | |
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | |
NSDate *d=[date dateFromString:theDate]; | |
NSTimeInterval late=[d timeIntervalSince1970]*1; | |
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; | |
NSTimeInterval now=[dat timeIntervalSince1970]*1; | |
NSString *timeString=@""; | |
NSTimeInterval cha=now-late; | |
if (cha/3600<1) { | |
if (cha/60<1) { | |
timeString = @"1"; | |
} | |
else | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/60]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
} | |
timeString=[NSString stringWithFormat:@"%@分钟前", timeString]; | |
} | |
else if (cha/3600>1&&cha/86400<1) { | |
timeString = [NSString stringWithFormat:@"%f", cha/3600]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@小时前", timeString]; | |
} | |
else if (cha/86400>1&&cha/864000<1) | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/86400]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@天前", timeString]; | |
} | |
else | |
{ | |
// timeString = [NSString stringWithFormat:@"%d-%"] | |
NSArray *array = [theDate componentsSeparatedByString:@" "]; | |
// return [array objectAtIndex:0]; | |
timeString = [array objectAtIndex:0]; | |
} | |
return timeString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment