objective-c – 当前日期和时间 – NSDate
发布时间:2020-12-16 03:28:01 所属栏目:百科 来源:网络整理
导读:我需要显示当前的日期和时间. 我用过 ; NSDate *currentDateNTime = [NSDate date]; 我想拥有当前的日期和时间(应该显示系统时间而不是GMT时间). 输出应该是NSDate格式而不是NSString. 例如; NSDate *currentDateNTime = [NSDate date];// Do the processing
我需要显示当前的日期和时间.
我用过 ; NSDate *currentDateNTime = [NSDate date]; 我想拥有当前的日期和时间(应该显示系统时间而不是GMT时间). 例如; NSDate *currentDateNTime = [NSDate date]; // Do the processing.... NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString 解决方法
由于所有NSDate都是GMT引用,您可能想要这样:
(不要忘记nowDate不会是实际的当前系统日期时间,但它会“移位”,所以如果你使用NSDateFormatter生成NSString,你会看到错误的日期) 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]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |