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 06-29-2011, 03:32 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 7
nagol2013 is on a distinguished road
Default Showing the date

Hi guys,
I am working on an app that uses the date to set off a chain reaction of events.
All I need help with is the code to tell the iPhone to take today's date and display it as a label.
Can anyone help with writing it? I am sure it is really simple but I can't find it anywhere!
Thanks for future help!
By the way I am a noobie at this but I am trying to get better by reading 3 books.
nagol2013 is offline   Reply With Quote
Old 06-29-2011, 04:45 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

1. Get today's date. Look at NSDate and one of it's class methods.

2. Create an NSDateFormatter.

3. Set the date format of the NSDateFormatter object to whatever you want. You can find a list of format codes here: Stepcase Blog Archive Format String for the iPhone NSDateFormatter

4. Turn the date into a string using one of NSDateFormatter's methods. Specifically stringFromDate.

5. If done, release the NSDateFormatter.
Domele is offline   Reply With Quote
Old 06-29-2011, 04:47 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

NSDate *today = [NSDate date];

that will get you today's date.

then to display it you need to put it into a string using NSDateFormatter (and maybe NSDateFormatterLongStyle)

and finally something like:

datelabel.text = [df stringFromDate:today];
RickSDK is offline   Reply With Quote
Old 06-29-2011, 08:03 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 7
nagol2013 is on a distinguished road
Default

I really dont want to ask this but i can not figure this out...
could any of you write the code for me?
nagol2013 is offline   Reply With Quote
Old 06-29-2011, 08:06 PM   #5 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Nobody is going to write the code for you. Anyone considering doing it please don't, it's not going to help him learn. Plus Rick already practically wrote it for you.
Domele is offline   Reply With Quote
Old 06-29-2011, 08:19 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 7
nagol2013 is on a distinguished road
Default

I can not understand the dateformatter part.
I am trying to write this for my mom and I need it to be done by the end of july and this is the last of the code i need. From there it is just copying and pasting over and over.
nagol2013 is offline   Reply With Quote
Old 06-29-2011, 08:40 PM   #7 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

First start off by creating a new NSDateFormatter object. Write the code here.
Domele is offline   Reply With Quote
Old 06-29-2011, 08:59 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 7
nagol2013 is on a distinguished road
Default

This is what I have so far and I dont even know if it works because it is saying expected identifier or ( before { and i can not get past this
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd"];
[formatter setDateStyle:NSDateFormatterMediumStyle];

NSString *dateToday = [formatter stringFromDate:[NSDate date]];

UILabel *dateLabel = [[UILabel alloc];
[dateLabel setText:dateToday];
[formatter release];
}
nagol2013 is offline   Reply With Quote
Old 06-29-2011, 09:14 PM   #9 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Okay your code looks good except you need to run initWithFrame on the label and you'd be creating a new label every time. I advise creating the label in IB and hooking an IBOutlet up. Then you just need to put a method name to it, I'm surprised that you didn't know how to do that but you knew how to write all that code. Also code tags next time please. [code] [1/CODE] Without the 1. So this is what it would look like with the label being created in IB and having a name of dateLabel.
Code:
- (void)setDateLabelWithCurrentDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd"];
[formatter setDateStyle:NSDateFormatterMediumStyle];

NSString *dateToday = [formatter stringFromDate:[NSDate date]];
[formatter release];
[dateLabel setText:dateToday];
}
Just call the method like so:
Code:
[self setDateLabelWithCurrentDate];
whenever you want to set the label.
Domele is offline   Reply With Quote
Old 06-30-2011, 12:24 AM   #10 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

Just my 2c:

I think the reason you get that error "expected identifier or ( before { " is because you didn't declare the method name at the front. --> If you follow what Domele said that should fix it.

I wish my skill is good enough and people start asking me to write code for them, at least not my mum ha ha....
Good luck and hope you meet your deadline...
new2objectivec is offline   Reply With Quote
Reply

Bookmarks

Tags
app, date, helping, iphone, label

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: 335
7 members and 328 guests
chemistry, iOS.Lover, lendo, leostc, Leslie80, pbart, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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