Display Current Date And Time in iPhone

In this application we will see how to display current date and time in iPhone. So let see how it will

Step 1: Open the Xcode, Create a new project using Single View Application. Give the application

Step 2: Need to add new viewController class in the project. Select project -> New file ->
UIViewController subclass -> next -> Give the application name “CurrentDateTimeView”.

Step 3: Now open the AppDelegate.h file and make the following changes:


#import <uikit/UIKit.h>
@class CurrentDateTimeView;
@interface AppDelegate : UIResponder <uiapplicationDelegate>
CurrentDateTimeView *currentDateTimeView;
@property (nonatomic, retain)IBOutlet CurrentDateTimeView *currentDateTimeView;
@property (strong, nonatomic) UIWindow *window;



Step 4: In the AppDelegate.m file make the following changes:


#import "AppDelegate.h"
#import "CurrentDateTimeView.h"
@implementation AppDelegate
@synthesize window = _window,currentDateTimeView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
currentDateTimeView = [[CurrentDateTimeView alloc]
[_window addSubview:currentDateTimeView.view];
return YES;



Step 5: Now open the “CurrentDateTimeView.h” file and make the following changes:

#import <uikit/UIKit.h>
@interface CurrentDateTimeView : UIViewController
IBOutlet UIButton *button;
NSString *string;
IBOutlet UILabel *dateLabel;
@property (nonatomic, retain) IBOutlet UIButton *button;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;
@property (nonatomic, retain) NSString *string;



Step 6: Double click the CurrentDateTimeView.xib file and open it to the Interface Builder. First
drag the UIButton and UILabel from the library and place it to the view window. Select the button
from the view window and bring up connection inspector and connect touch up inside to the File's
Owner icon and select “currentdate:” method and connect the File's owner icon to the label and
select “dateLabel”. Now save the .xib file, close it and go back to the Xcode.

Step 7: In the CurrentDateTimeView.m file make the following changes:


#import "CurrentDateTimeView.h"
@implementation CurrentDateTimeView
@synthesize button, string, dateLabel;
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
[dateLabel setText:str];
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);



Step 8: Now compile and run the application on the Simulator.