Skip to content

Instantly share code, notes, and snippets.

@koogawa
Created April 14, 2014 07:25
Show Gist options
  • Save koogawa/10624048 to your computer and use it in GitHub Desktop.
Save koogawa/10624048 to your computer and use it in GitHub Desktop.
Dateから相対日時を返す
+ (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