Home Tutorial Iphone Examples Iphone Show Current Date & Time

 
 

Share on Google+Share on Google+
Iphone Show Current Date & Time
Posted on: September 5, 2009 at 12:00 AM
Advertisement
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);

    //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];

    [button release];

    [super dealloc];

}

 @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

Advertisement

Related Tags for Iphone Show Current Date & Time:


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 5, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com