0

iOSで特定の日付をシステムタイムゾーンに変換しているときに問題が発生しました。

ソース日付:2013-03-18 03:54:18 // AM形式で宛先日付は:2013-03-18 03:30:15//PM形式である必要があります。

どうすればこれを入手できますか?

私は以下のコードを試しましたが、間違ったデータと違いも取得しています。

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSLog(@"Post date::%@",postDate);
NSDate *utc = [fmt dateFromString:postDate];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(@" local %@", local);

NSDate *date1 = [NSDate date];
NSString *currentDateString = [fmt stringFromDate:date1];
NSDate *currentDate = [fmt dateFromString:currentDateString];
NSUInteger flags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *difference = [[NSCalendar currentCalendar] components:flags fromDate:utc toDate:currentDate options:0];
NSLog(@"difference::%d",[difference day]);
NSLog(@"difference::%d",[difference hour]);
NSLog(@"difference::%d",[difference minute]);

編集:出力

Post date::2013-03-18 03:20:09 
local 2013-03-18 03:20:09 
difference::0 
difference::13 
difference::2
4

2 に答える 2

1

コードを次から置き換えます...

fmt.timeZone = [NSTimeZone systemTimeZone];

このように

fmt.timeZone = [NSTimeZone localTimeZone];

これはあなたのために働くでしょう...

于 2013-03-18T11:21:07.210 に答える
0
NSString *openingHour = @"15:05:35 ";
 NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init]; 
        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
        [workingHoursFormatter setTimeZone:timeZone];
        [workingHoursFormatter setDateFormat:@"HH:mm"];
        NSDate *openingTime = [workingHoursFormatter dateFromString:openingHour];

上記のコードを試してみてください。必要に応じて日付形式を変更してください。

于 2013-03-18T11:36:47.747 に答える