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

ここでの問題は、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

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

Well, I've sorted it out this way:

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

Then, anothe method calculate the difference between two NSDates, which is a figure in milliseconds -a float

- (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 に答える