You have to do this in app delegate's function
- (void)applicationDidEnterBackground:(UIApplication *)application ;
or
- (void)applicationWillTerminate:(UIApplication *)application ;
To get date and time (hours and seconds also)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *formattime=[dateFormatter stringFromDate:[NSDate date]];
NSArray *chunks = [formattime componentsSeparatedByString:@":"];
NSArray *sec = [[chunks objectAtIndex:2] componentsSeparatedByString:@" "];
//if([[sec objectAtIndex:1] isEqualToString:@"PM"]){
int hour=[[chunks objectAtIndex:0] intValue];
int min= [[chunks objectAtIndex:1] intValue];
//pmoram=[sec objectAtIndex:1];
int second=[[sec objectAtIndex:0] intValue];
//Use the date and release the dateFormatter
[dateFormatter release];