LoginSignup
37
36

More than 5 years have passed since last update.

iOSアプリで「〜日前」表示

Last updated at Posted at 2013-02-24

対象日時のunixタイムスタンプをを渡すと
「〜日前」「〜時間前」「〜分前」表示に変換

- (NSString *)_displayDate:(int)timestamp
{
    NSDate *now = [NSDate date];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)timestamp];
    NSTimeInterval passed = [now timeIntervalSinceDate:date];
    int min = passed / 60;
    if (min <= 1) {
        return @"1分前";
    }
    if (min < 60) {
        return [NSString stringWithFormat:@"%d分前", min];
    }
    int hour = min / 60;
    if (hour < 24) {
        return [NSString stringWithFormat:@"%d時間前", hour];
    }
    int day = hour / 24;
    return [NSString stringWithFormat:@"%d日前", day];
}
37
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
36