Time since creation in seconds converted to readable time - Objective-C [duplicate]
Possible Duplicate:
iPhone: Convert date string to a relative time stamp
I have a problem. I have tried creating a way to get a time since stamp such as "14 hours ago" but it doesn't seem to be working. So I need to help to create a function that takes seconds from the time a database row/item was created. So if the db row was created 600 seconds ago that would mean the function would output "10 minutes ago"? I want it to return either seconds, minutes, hours, weeks, months or years. I know its probably really simple, I just can't seem to get it right.开发者_C百科..
OH and this is for an iPhone app so it uses objective-c.
Any help would be much appreciated.
THIS IS WHAT I HAVE CURRENTLY:
-(NSString *)timeSinceTimestamp:(NSString *)seconds{
double seconds2 = [seconds doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds2];
NSDate *now = [NSDate date];
double start = [date timeIntervalSince1970];
double end = [now timeIntervalSince1970];
double difference = (end - start) / 1000;
difference = round(difference);
int minutes = difference / 60;
int hours = minutes / 60;
int days = hours / 24;
int weeks = days / 7;
int months = weeks / 5;
int years = months / 12;
NSString *string;
if(difference < 60){
string = [NSString stringWithFormat:@"%i seconds ago",difference];
}else if (minutes > 1 && minutes < 60) {
string = [NSString stringWithFormat:@"%i minutes ago",minutes];
}else if (hours > 1 && hours < 24) {
string = [NSString stringWithFormat:@"%i hours ago",hours];
}else if (days > 1 && days < 7) {
string = [NSString stringWithFormat:@"%i days ago",days];
}else if (weeks > 1 && weeks < 5) {
string = [NSString stringWithFormat:@"%i weeks ago",weeks];
}else if (months > 1 && months < 12) {
string = [NSString stringWithFormat:@"%i months ago",months];
}else if (years > 1 && years < 12) {
string = [NSString stringWithFormat:@"%i years ago",years];
}
return string;
}
Calculate how many seconds your delta is. Use NSDate dateWithIntervalSince1970 or dateWithIntervalSinceReferenceDate to create an NSDate object from the seconds value. You can format that value into hours/minutes/seconds with NSDateFormatter. The month and year will be wrong but who cares?
To calculate days and weeks the simplest thing to do is to divide the seconds by 24 * 60 * 60 to get days, then divide that by 7 to get weeks. Months and years are a problem since they are irregular, so you'd probably be better off to use calendrical calculations if you want that level of resolution. If you know that something was created 10000 seconds ago you can use NSDate dateWithTimeIntervalSinceNow (with a negative interval) to produce the date object for that earlier time.
[By the way, your code above appears to be roughly correct (I didn't go over it closely). I don't see any reason why you couldn't make it work, with a little effort.]
The documentation has a whole section on Calendrical Calculations.
If you have the creation date - and you have the number of seconds elapsed since the creation date - you can calculate the new date by using the methods described in that document.
精彩评论