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 12-12-2010, 02:13 PM   #1 (permalink)
iPhone Newbie
 
Join Date: Dec 2010
Location: Worcestershire, UK
Age: 24
Posts: 66
mwyld is on a distinguished road
Default Simple Question: NSDate

Hi, still quite a newbie to iPhone developer aso bear with me.
All I'm trying to do is display today's month and day in a label on the nib

the connections to the nib labels are ok but what am I doing wrong here?

Code:
//NSDate *today = [NSDate date];
	NSDateComponents *comp = [[NSDateComponents alloc] init];
	NSInteger month = [comp month];
	NSInteger day = [comp day];
	
	NSString *monthTxt;
	
	if (month == 1) {
	    monthTxt = @"Jan";
	} else if (month == 2) {
		monthTxt = @"Feb";
	} else if (month == 3) {
		monthTxt = @"Mar";
	} else if (month == 4) {
		monthTxt = @"Apr";
	} else if (month == 5) {
		monthTxt = @"May";
	} else if (month == 6) {
		monthTxt = @"Jun";
	} else if (month == 7) {
		monthTxt = @"Jul";
	} else if (month == 8) {
		monthTxt = @"Aug";
	} else if (month == 9) {
		monthTxt = @"Sep";
	} else if (month == 10) {
		monthTxt = @"Oct";
	} else if (month == 11) {
		monthTxt = @"Nov";
	} else if (month == 12) {
		monthTxt = @"Dec";
	} else {
		monthTxt = @"Err";
	}
	
	[lblMonth setText:monthTxt];
	[lblDay setText:[NSString stringWithFormat:@"%i",day]];
	
	[monthTxt release];
	[comp release];
mwyld is offline   Reply With Quote
Old 12-12-2010, 02:29 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

To be able to help, you need to tell us what happens when you run the code, does it give an error (which one and on which line), does it not work as you expect (what does it do and what did you expect it to do).

Also, I think you might be able to use the NSDateFormatter to give you the month name as a string.

Last edited by baja_yu; 12-12-2010 at 02:31 PM.
baja_yu is offline   Reply With Quote
Old 12-12-2010, 02:32 PM   #3 (permalink)
Registered Member
 
newDev's Avatar
 
Join Date: Jul 2010
Location: Utah
Posts: 75
newDev is on a distinguished road
Default

You should use NSDateFormatter to display NSDate in a format you'd like.
Check the developer documentation.

Edit: baja_yu beat me to it.

also try this
Code:
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	
	NSDate *date = [NSDate date];
	
	NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"];
	[dateFormatter setLocale:usLocale];
	
	NSLog(@"Date: %@",[dateFormatter stringFromDate:date]);

Last edited by newDev; 12-12-2010 at 02:56 PM.
newDev is offline   Reply With Quote
Old 12-12-2010, 04:14 PM   #4 (permalink)
iPhone Newbie
 
Join Date: Dec 2010
Location: Worcestershire, UK
Age: 24
Posts: 66
mwyld is on a distinguished road
Default

thanks both for your help. newDev how would I extract just the month from this or just the day?
mwyld is offline   Reply With Quote
Old 12-12-2010, 04:33 PM   #5 (permalink)
Registered Member
 
newDev's Avatar
 
Join Date: Jul 2010
Location: Utah
Posts: 75
newDev is on a distinguished road
Default

Sorry, didnt realize you only wanted the month and day
Try this instead,
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
		// read out the format string
	NSString *format = [dateFormatter dateFormat];
	format = [format stringByReplacingOccurrencesOfString:@"y" withString:@""];
	format = [format stringByReplacingOccurrencesOfString:@"," withString:@""];

	[dateFormatter setDateFormat:format];
	
	NSDate *date = [NSDate date];
	
	NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
	[dateFormatter setLocale:usLocale];
	
	NSLog(@"Date: %@",[dateFormatter stringFromDate:date]);
newDev is offline   Reply With Quote
Reply

Bookmarks

Tags
nsdate, simple

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: 383
7 members and 376 guests
chemistry, daudrizek, HemiMG, jeroenkeij, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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