Objective c date: NSString Date


 

Objective c date: NSString Date

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.

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"

0

@implementation dateAppDelegate;

@synthesize window, button, string, dateLabel;

-(IBAction)currentdate:(id)sender

1

{

        NSDate* date = [NSDate date];

        //Create the dateformatter object

2

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

        //Set the required date format

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

3

        //Get the string date

        NSString* str = [formatter stringFromDate:date];

        //Display on the console

4

        NSLog(str);

        //Set in the lable

        [dateLabel setText:str];

5

}

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

    // Override point for customization after application launch

6

    [window makeKeyAndVisible];

}

- (void)dealloc {

7

    [dateLabel release];

    [window release];

    [super dealloc];

8

}

@end

The NSString date application in iphone will look like this:

9

Click to Download Code

Ads