1

我正在开发一个应用程序,我想在用户设置的时间触发本地通知,我只是在获取正确时间方面遇到问题,所以我在做什么是这样的:

NSDateFormatter *current = [[NSDateFormatter alloc] init];
[current setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[current setDateFormat:@"HH:MM:SS"];
NSDate *systemTime = [NSDate date];
[current setTimeZone:[NSTimeZone systemTimeZone]];

NSLog(@"Current time %@" ,[current stringFromDate:systemTime]);
temp = [timeSetter date];//this temp is NSDATE object

NSLog(@"USer time %@" ,[current stringFromDate:temp]);
value = [temp timeIntervalSinceDate:systemTime];//This value is Nstimeinterval instance/obj.

当我通过 NSLog 打印这些是我收到的结果现在你可以 c 23.11.31.323 是实际系统时间,我得到 23.02.32。 请帮助我,在此先感谢

4

1 回答 1

1

我刚刚运行了您的代码,并重现了类似的结果。你的问题出在-setDateFormat:方法上——应该是[current setDateFormat:@"HH:mm:ss"]——这对我来说没问题。在日期格式字符串中,M = 月(02 = 二月)、m = 分钟、S = 秒的小数部分和 s = 秒。

如果您查看有关日期格式的 Apple 文档,您会发现格式字符串是在 Unicode 标准中确定的。它们链接到各种版本;最新的在这里

于 2013-02-16T22:29:56.237 回答