保存执行事件时的当前时间

时间:2012-10-04 05:10:45

标签: iphone time zbar-sdk

在我的应用程序中,我使用zbar sdk扫描票证,我想在相机拍摄时保存sqlite中的当前时间。我怎样才能做到这一点。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

以下代码用于获取设备本地当前时间。

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

这可以为编码提供很多帮助

答案 1 :(得分:1)

你可以这样写:

NSDate *currentTime = [NSDate date];

然后,如果需要,可以使用NSDateComponentsNSDateFormatter从NSDate对象中提取适当的值。

答案 2 :(得分:0)

当您在ZBarReaderController上单击“保存”时,此方法称为..

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

}

还有关于NSDate的更多细节,请参阅我在博客上的链接..

http://parasjoshi3.blogspot.in/2012/01/convertstringtodatedatetostring.html

我希望这可以帮助你...

:)