Starting with a unix timestamp like 1290529723, how would I (assuming gmt) get the information on whether it is:
today (if so, what time)
i
It takes a bit of fiddling to get a solution that respects the device's locale. The following method relativeStringFromDate returns a string representing the date, formatted as following:
just the date component if the date is over one week ago, according to locale (e.g. '1/20/2012' in US vs '20/1/2012' in Europe)
- (NSString *)relativeStringFromDate:(NSDate *)date {
if ([self dateIsToday:date])
return [self dateAsStringTime:date];
else if ([self dateIsYesterday:date])
return [self dateAsStringDate:date];
else if ([self dateIsTwoToSixDaysAgo:date])
return [self dateAsStringDay:date];
else
return [self dateAsStringDate:date];
}
- (BOOL)date:(NSDate *)date
isDayWithTimeIntervalSinceNow:(NSTimeInterval)interval {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *other_date;
other_date = [NSDate dateWithTimeIntervalSinceNow:interval];
NSString *d1, *d2;
d1 = [df stringFromDate:date];
d2 = [df stringFromDate:other_date];
return [d1 isEqualToString:d2];
}
- (BOOL)dateIsToday:(NSDate *)date {
return [self date:date isDayWithTimeIntervalSinceNow:0];
}
- (BOOL)dateIsYesterday:(NSDate *)date {
return [self date:date isDayWithTimeIntervalSinceNow:-86400];
}
- (BOOL)dateIsTwoToSixDaysAgo:(NSDate *)date {
for (int i = 2; i <= 6; i += 1)
if ([self date:date isDayWithTimeIntervalSinceNow:i*-86400])
return YES;
return NO;
}
- (NSString *)dateAsStringDate:(NSDate *)date {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
[df setDoesRelativeDateFormatting:YES];
NSString *str = [df stringFromDate:date];
return str;
}
- (NSString *)dateAsStringTime:(NSDate *)date {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterShortStyle];
NSString *str = [df stringFromDate:date];
return str;
}
- (NSString *)dateAsStringDay:(NSDate *)date {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEEE"];
NSString *str_day = [df stringFromDate:date];
return str_day;
}
As mentioned in yours and Brad's answers, you can obtain an NSDate from a timestamp using NSDate's dateWithTimeIntervalSince1970.