Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从文本视图到NSDate的时间_Objective C_Datetime_Nsdateformatter - Fatal编程技术网

Objective c 从文本视图到NSDate的时间

Objective c 从文本视图到NSDate的时间,objective-c,datetime,nsdateformatter,Objective C,Datetime,Nsdateformatter,我正在开发一款iPhone应用程序来计算时间:一辆车必须在给定的时间点到达,所以如果车迟早会到达,就会得到分数 我需要尽可能获得最精确的计时,所以我想使用毫秒,但结果令人失望 时间是在文本字段中键入的,比如说12:17:22,代码如下: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; [dateForma

我正在开发一款iPhone应用程序来计算时间:一辆车必须在给定的时间点到达,所以如果车迟早会到达,就会得到分数

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

时间是在文本字段中键入的,比如说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


提前谢谢

如下设置NSDateFormatter的属性(替换为时区信息):


这里的问题是,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
 2000-01-01 12:12:12 +0000
编辑:当记录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);
我会给你

timeLegStart = 12:12:12
 2000-01-01 12:12:12 +0000

快乐编码:)

好吧,我是这样分类的:

- (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];
谢谢大家的时间和建议。
路易斯

你得到的是格林尼治时间而不是当地时间。你需要设置日期或格式化程序的时间线,我忘了是哪一个。哦,天哪,你这么做了!但我仍然认为这是一个时区问题,所以在这种情况下这样做是不正确的并检查。@Jeremy,字符串是“setStartTime.text”,我需要它是NSDate来计算NSTimeInterval。抱歉,它不起作用。无论如何,我需要NSDate来计算NSTimeInterval。你确定它不起作用吗?因为我在一个新的项目中使用了它。@Luis:我不明白你的意思。你想用你输入的字符串作为时间来表示今天的日期吗?这也是我得到的;我只想和时间打交道,不想和日期打交道;但我认为,如果我使用时间和日期来获取时间间隔,这真的不重要。我的意思是,从时间1=12:22:23到时间2=12:22:24的时间差与时间1=2013-02-08 12:22:23+0000到时间2=2013-02-08 12:22:24+0000的时间差是一样的,不是吗?但我仍然需要格式化时间以使用毫秒。干杯