Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2011, 05:46 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 43
mediatagging is on a distinguished road
Default Compare Dates return time elapsed

I have a date stored in a NSString in the following format: 2011-02-20 14:04:25

I need a function that will:

Take the current time and subtract the given time from it.

If the result is less than seconds then display it as X seconds ago.
if the result is less than an hour display it as X minutes ago.
if the result is less than a day display it as X hours ago
all other results should be returned as X days ago.

P.S. I have it up on craigslist as i'm also willing to pay.
link: Objective C - Date function
thanks
mediatagging is offline   Reply With Quote
Old 02-20-2011, 07:22 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I only ran a few tests, but this seems correct. It does not handle dates in the future.

Code:
-(NSDate*)dateFromString:(NSString*)dateString{
	// Convert string to date object
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"yyyy-MM-dd H:mm:ss"];
	NSDate *date = [dateFormat dateFromString:dateString];  
	[dateFormat release];
	
	return date;
}

#define kMinute 60
#define kHour (60*60)
#define kDay (60*60*24)
-(NSString*)textFromSeconds:(double)seconds{
	if(seconds < kMinute)
		return [NSString stringWithFormat:@"%0.0f seconds",seconds];
	
	if(seconds < kHour)
		return [NSString stringWithFormat:@"%0.0f minutes",seconds/kMinute];
	
	if(seconds < kDay)
		return [NSString stringWithFormat:@"%0.0f hours",seconds/kHour];
	
	return [NSString stringWithFormat:@"%0.0f days", seconds/kDay];
}

-(NSString*)textFromDateString:(NSString*)dateString{
	NSDate *date =[self dateFromString:dateString];
	double seconds = [[NSDate date] timeIntervalSinceDate:date];
	NSString *text = [self textFromSeconds:seconds];
	NSLog(@"text is: %@", text);
	return text;
}

-(void)test{
	[self textFromDateString:@"2011-02-20 17:19:25"];
	[self textFromDateString:@"2011-02-20 17:10:25"];
	[self textFromDateString:@"2011-02-20 14:04:25"];
	[self textFromDateString:@"2011-02-19 14:04:25"];
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-20-2011, 09:10 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 43
mediatagging is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
I only ran a few tests, but this seems correct. It does not handle dates in the future.

Code:
-(NSDate*)dateFromString:(NSString*)dateString{
	// Convert string to date object
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"yyyy-MM-dd H:mm:ss"];
	NSDate *date = [dateFormat dateFromString:dateString];  
	[dateFormat release];
	
	return date;
}

#define kMinute 60
#define kHour (60*60)
#define kDay (60*60*24)
-(NSString*)textFromSeconds:(double)seconds{
	if(seconds < kMinute)
		return [NSString stringWithFormat:@"%0.0f seconds",seconds];
	
	if(seconds < kHour)
		return [NSString stringWithFormat:@"%0.0f minutes",seconds/kMinute];
	
	if(seconds < kDay)
		return [NSString stringWithFormat:@"%0.0f hours",seconds/kHour];
	
	return [NSString stringWithFormat:@"%0.0f days", seconds/kDay];
}

-(NSString*)textFromDateString:(NSString*)dateString{
	NSDate *date =[self dateFromString:dateString];
	double seconds = [[NSDate date] timeIntervalSinceDate:date];
	NSString *text = [self textFromSeconds:seconds];
	NSLog(@"text is: %@", text);
	return text;
}

-(void)test{
	[self textFromDateString:@"2011-02-20 17:19:25"];
	[self textFromDateString:@"2011-02-20 17:10:25"];
	[self textFromDateString:@"2011-02-20 14:04:25"];
	[self textFromDateString:@"2011-02-19 14:04:25"];
}
This is perfect, thank you.
mediatagging is offline   Reply With Quote
Reply

Bookmarks

Tags
comparison, data

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 377
14 members and 363 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0