Iphone Show Current Date & Time


 

Iphone Show Current Date & Time

In the previous examples, we have printed current date and time separately in iphone simulator. But in this example we are going to show you how to print both current date and current time in iphone.

In the previous examples, we have printed current date and time separately in iphone simulator. But in this example we are going to show you how to print both current date and current time in iphone.

Iphone Show Current Date & Time


In the previous examples, we have printed current date and time separately in iphone simulator. But in this example we are going to show you how to print both current date and current time in iphone.

The code is almost same...as in the previous current date example. The only difference is that we have changed in the format for example ...
earlier it was: [formatter setDateFormat:@"yyyy-MM-dd"]; in date example
and in current time, it was written as: [formatter setDateFormat:@"HH:MM:SS"];

Now to print combined result, it should be written like this: [formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];

The output of the application will look like this: 


Example Code:
datetimeAppDelegate.h

#import <UIKit/UIKit.h>

 @interface datetimeAppDelegate: 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

  datetimeAppDelegate.m

 #import "datetimeAppDelegate.h"

 @implementation datetimeAppDelegate;

 @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 HH:MM:SS"];

    //Get the string date

    NSString* str = [formatter stringFromDate:date];

    //Display on the console

    NSLog(str);

0

    //Set in the lable

    [dateLabel setText:str];

 }

1

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

     // Override point for customization after application launch

    [window makeKeyAndVisible];

2

}

 - (void)dealloc {

    [dateLabel release];

3

    [window release];

    [button release];

    [super dealloc];

4

}

 @end

There is one more way to write the code to display the current date and time in phone application. please see the code given below...

-(IBAction)time:(id)sender;
{

NSDate *dateTime = [NSDate date];
NSString *clock = [[NSString alloc] initWithFormat:@"%@", dateTime];
NSLog(clock);

[dateLabel setText:clock];
[clock release]; 

}

In this code, we have created a NSDate object and clock object to show the local time. "dateLabel" is a name of lable on which we are going to print the returned value. then release the clock.

Click to Download Code

5

Ads