2

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

4

3 回答 3

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

愿这对编码有很大帮助

于 2012-10-04T05:17:34.793 回答
1

你可以这样写:

NSDate *currentTime = [NSDate date];

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

于 2012-10-04T05:19:58.873 回答
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/convertstringtodatetostring.html

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

:)

于 2012-10-04T05:31:11.090 回答