7

現在の日付と時刻を表示する必要があります。

利用した ;

NSDate *currentDateNTime        = [NSDate date];

現在の日付と時刻が欲しい(GMT時刻ではなくシステム時刻を表示する必要があります)。出力は、NSDateではなくフォーマットである必要がありますNSString

例えば;

    NSDate *currentDateNTime        = [NSDate date];
// Do the processing....

NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString
4

5 に答える 5

9

すべての NSDate は GMT 参照であるため、おそらくこれが必要です:間違った日付)

NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];

NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
于 2012-05-28T16:43:45.200 に答える
0

日付の文字列を取得するには、NSDateFormatter が必要であり、stringFromDate このメソッドを呼び出します。

NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformater setDateFormat:@"yyyyMMdd,HH:mm"];
NSString *str = [dateformater stringFromDate: currentDateNTime];
于 2012-05-28T16:39:23.050 に答える