Created
April 14, 2014 07:25
-
-
Save koogawa/10624048 to your computer and use it in GitHub Desktop.
Dateから相対日時を返す
This file contains 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
+ (NSString *)relativeDateStringFromDate:(NSDate *)date | |
{ | |
NSInteger sec = (int)[[NSDate date] timeIntervalSinceDate:date]; | |
if (sec < 60) { | |
return @"たった今"; | |
} | |
else if (sec < 120) { | |
return @"1分前"; | |
} | |
else if (sec < 3600) { | |
return [NSString stringWithFormat:@"%d分前", sec / 60]; | |
} | |
else if (sec < 7200) { | |
return @"1時間前"; | |
} | |
else if (sec < 86400) { | |
return [NSString stringWithFormat:@"%d時間前", sec / 3600]; | |
} | |
else if (sec < 172800) { | |
return @"1日前"; | |
} | |
else { | |
return [NSString stringWithFormat:@"%d日前", sec / 86400]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment