0

我正在构建一个 iPhone 应用程序来花费时间:一辆车必须在给定的时间点到达,所以如果汽车迟早要获得积分。

我需要尽可能获得最精确的时间,所以我想使用毫秒,但结果令人失望。

时间是在 TextField 上输入的,比如说12:17:22,代码如下:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
 [dateFormatter setDateFormat:@"HH:mm:ss"];
 
timeLegStart = [NSDate new];  
timeLegStart = [dateFormatter dateFromString:setStartTime.text];

NSLog(@"timeLegStart = %@",timeLegStart);

输出是1970-01-01 11:17:22 +0000,我希望它是 12:17:22:000

4

3 回答 3

0

像这样设置您的 NSDateFormatter 的属性(替换为您的时区信息):

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Sao_Paulo"]];
[dateFormatter setDateFormat:@"HH':'mm':'ss' 'z"];
[dateFormatter setLocale:locale];
于 2013-02-07T12:10:35.110 回答
0

这里的问题是 NSDate 包含日期和时间,它们不是单独可用的。当您仅提供带有时间规范的输入日期时,它会使用您提供的时间加载日历开始日期值并创建一个日期。因此,这里将日期作为输出。实际上,使用它没有问题。

您可以使用相同的格式化程序来获取检索到的时间:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate* timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:@"12:12:12"];
NSLog(@"timeLegStart = %@",[dateFormatter stringFromDate:timeLegStart]);

它会给你:

timeLegStart = 12:12:12

编辑:在记录 NSDate 时,它​​将仅采用 GMT 格式。由于您将时区设置为本地,因此日期将始终像您得到的那样显示。

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSDate* timeLegStart = [NSDate new];
    timeLegStart = [dateFormatter dateFromString:@"12:12:12"];
    NSLog(@"timeLegStart = %@",timeLegStart);

会给你:

 2000-01-01 12:12:12 +0000
于 2013-02-07T12:38:12.727 回答
0

好吧,我是这样整理的:

- (void)fijaTiempoInicio
    {
    //getting current day
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~ NSTimeZoneCalendarUnit fromDate:[NSDate date]];
     [dateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Spain"]];
    NSDate *currentDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

    //extracting year, month and day from current date
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"dd"];
    NSString *currentDayString = [NSString stringWithFormat:@"%@",[df stringFromDate:currentDate]];

    [df setDateFormat:@"MM"];
    NSString *currentMonthString = [NSString stringWithFormat:@"%@",[df stringFromDate:currentDate]];

    [df setDateFormat:@"yyyy"];
    NSString *currentYearString = [NSString stringWithFormat:@"%@", [df stringFromDate:currentDate]];

    NSDateFormatter *ft = [[NSDateFormatter alloc]init];

    [ft setTimeZone:[NSTimeZone localTimeZone]];
    [ft setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *tempDate = [NSString stringWithFormat:@"%@-%@-%@ %@",currentYearString,currentMonthString,currentDayString,txtTiempoInicioTramo.text];
NSLog(@"tempDate= %@",tempDate);

    startTime = [ft dateFromString:tempDate];
    NSLog(@"startTime= %@",startTime);

然后,另一种方法计算两个 NSDate 之间的差异,这是一个以毫秒为单位的数字 - 一个浮点数

- (IBAction)btnCapturarTiempo:(id)sender 
    {

    NSDateFormatter *formatCarTime = [[NSDateFormatter alloc]init];
    [formatCarTime setDateFormat:@"HH:mm:ss"];
    [formatCarTime setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Madrid"]];
 
    carTime =  [NSDate date];
    difference = [carTime timeIntervalSinceDate:startTime];
    labelTiempoCoche.text = [NSString stringWithFormat:@"%f",difference];
于 2013-02-19T19:38:48.553 回答