9

i'm developing a tool for car driving which saves GPS Data, Accelerometer Data and Time for later use.

Acceleration is the least Priority, though.

All i need is a little code example so i can get the current system time printed as an updating clock in Xcode because i want it to save my data as fast and as often as possible :>

Thanks in advance for any answers :)

h4wkeye

h4wkeye
  • 369
  • 2
  • 4
  • 9

3 Answers3

16

To get the current system time use

NSDate* currentDate = [NSDate date];

and To get the string representation of NSDate

- (NSString *)description

Then Use below

NSString* dateInString = [currentDate description];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • How do i add this? i'm pretty new to xCodeand i eed to know what of this is typed into the header and what into the source...sorry if it's much to ask :> – h4wkeye Apr 12 '11 at 10:53
  • @Jhaliya So where to implement which of the lines? a dumb question i guess but i won't learn without asking :> – h4wkeye Apr 12 '11 at 11:02
  • @h4wkeye: you need to put the in code in source file ... i would suggest to read objective-C doc's provided by Apple .. will be much helpful for you in the beginning ... – Jhaliya - Praveen Sharma Apr 12 '11 at 11:04
  • @Jhaliya Thanks :) Only the first Line of your Code is working though, second line gives me "description" undeclared, third one says "expected ; at the end" – h4wkeye Apr 12 '11 at 11:08
  • @h4wkeye: Use as below NSDate* currentDate = [NSDate date]; NSString* dateInString = [currentDate description]; NSlog(@"System Current %@", dateInString); – Jhaliya - Praveen Sharma Apr 12 '11 at 11:42
  • @Jhaliya i need GMT +2 though :/ – h4wkeye Apr 12 '11 at 12:03
  • Edit it's not GMT but its 2 hrs back, 14.10 here but clock says 12:10 :/ – h4wkeye Apr 12 '11 at 12:10
  • @h4wkeye Plz look into the below SO post for getting date in GMT http://stackoverflow.com/questions/1490537/gmt-time-on-iphone – Jhaliya - Praveen Sharma Apr 12 '11 at 12:16
  • @Jhaliya i can#t get that stuff inside your link to work, is there no way to just set the friggin time to +0200? i know this preference is located inside "description" but how do i change it? D: – h4wkeye Apr 12 '11 at 13:01
11

Like to share this simple code example. Just put this any where in your myClass code. You May find more formatting examples > Format String for the iPhone NSDateFormatter.

// Get current date time

NSDate *currentDateTime = [NSDate date];

// Instantiate a NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// Set the dateFormatter format

//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// or this format to show day of the week Sat,11-12-2011 23:27:09

[dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];

// Get the date time in NSString

NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];

NSLog(@"%@", dateInStringFormated);

// Release the dateFormatter

[dateFormatter release]; 
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
Helmut Taylor
  • 111
  • 1
  • 2
3

Use this code in viewDidLoad or viewWillAppear

-(void)callUpdate
{
NSTimer *timer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:0.1 target:self     selector:@selector(updateTimer) userInfo:nil repeats:YES];

}
-(void)updateTimer
{
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];

[dateformatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];

NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate date]];
label.text = dateInStringFormated;

}

or else just alloc the timer in the viewDidLoad or viewWillAppear and when you need to stop the timer use

[timer invalidate];
ashokdy
  • 1,001
  • 12
  • 21