2

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.

iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54

3 Answers3

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