In my app i am using zbar sdk to scan ticket and i want to save current time in sqlite when the camera takes the shot . How can i do this. Thanks in advance.
Asked
Active
Viewed 102 times
2
-
1[How to get current time?](http://stackoverflow.com/a/4111770/1603234) – Hemang Oct 04 '12 at 05:18
3 Answers
1
Following code use for the getting the device local current time.
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
May this Helping a lot for coding

Nimit Parekh
- 16,776
- 8
- 50
- 72
1
You can write something like this:
NSDate *currentTime = [NSDate date];
Then you can extract appropriate values from NSDate object by using NSDateComponents
or NSDateFormatter
, if you need.

rmxmaster
- 156
- 15
0
when you click save on ZBarReaderController , this bellow method called..
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
NSDate *currentDateandTime = [NSDate date];
/// and save this date and time in your database
////with date formate see bellow my edited answer
/// this for get string from date with formate
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd-MM-yyyy hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(dateString);
/// for get date from string with different formate use bellow code
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];
NSDate *date = [dateFormatter dateFromString: dateString];
}
also for more detail about NSDate see my blog on bellow link..
http://parasjoshi3.blogspot.in/2012/01/convertstringtodatedatetostring.html
i hope this help you...
:)

Paras Joshi
- 20,427
- 11
- 57
- 70