Home Tutorial Iphone Examples Objective c date: NSString Date

 
 

Share on Google+Share on Google+
Objective c date: NSString Date
Posted on: September 4, 2009 at 12:00 AM
Advertisement
In the given iPhone objective c date functions example, we are going to show you how to print the current date using NSDate of NSString class. To print the current date on iphone we required, a label(to print the date) and button(for action), connect them with file owner.

Objective c date: NSString Date

In_ the given objective c date functions example, we are going to show you how to print the current date using NSDate of NSString class. To print the current date on iphone we required, a lable(to print the date) and button(for action), connect them with file owner. 

Once you done, you need to write a date method to show the current date on button action. 

NSdate Method:

NSDate* date = [NSDate date];

        //Create the dateformatter object

        NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

        //Set the required date format

        [formatter setDateFormat:@"yyyy-MM-dd"];

        //Get the string date

        NSString* str = [formatter stringFromDate:date];

        //Display on the console

        NSLog(str);

        //Set in the lable

        [dateLabel setText:str];

Note: it must be written into the button action. 

Code explanation:
In the first line: "NSDate* date = [NSDate date];" we are creating an object. In the second line of code "NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
" we have created the dateformatter of object, and used alloc method that will be called on NSString itself. It's basically used to reserve memory and instantiates an object. we have also used "init" that will create instance variable. In the third line we have created a formatter in which we wanted to display the date.

and in the last NSLog is used to display the result.

Code: 

dateAppDelegate.h

#import <UIKit/UIKit.h>

@interface dateAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

    IBOutlet UIButton *button;

    NSString *string;

    IBOutlet UILabel *dateLabel;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UIButton *button;

@property (nonatomic, retain) IBOutlet UILabel *dateLabel;

@property (nonatomic, retain) NSString *string;

-(IBAction)currentdate:(id)sender;

@end

dateAppDelegate.m

#import "dateAppDelegate.h"

@implementation dateAppDelegate;

@synthesize window, button, string, dateLabel;

-(IBAction)currentdate:(id)sender

{

        NSDate* date = [NSDate date];

        //Create the dateformatter object

        NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

        //Set the required date format

        [formatter setDateFormat:@"yyyy-MM-dd"];

        //Get the string date

        NSString* str = [formatter stringFromDate:date];

        //Display on the console

        NSLog(str);

        //Set in the lable

        [dateLabel setText:str];

}

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    // Override point for customization after application launch

    [window makeKeyAndVisible];

}

- (void)dealloc {

    [dateLabel release];

    [window release];

    [super dealloc];

}

@end

The NSString date application in iphone will look like this:

Click to Download Code

Advertisement

Related Tags for Objective c date: NSString Date:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 4, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com