NSString 的 NSDate 给出 null 结果
我正在使用以下代码生成 NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(@"%@",str);
return str;
}
上面的代码一切都很好。我正在使用上面的代码将字符串存储在数据库中。现在,在检索该字符串时,我得到了 NULL
。以下是我以特定格式检索日期的代码
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(@"Date : %@",dt);
[dateFormatter release];
我应该如何以特定格式检索或存储?我的 crdInfo.swipeTime 正在正确检索字符串...
I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(@"%@",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL
. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(@"Date : %@",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,为什么不只存储 NSDate 对象或纪元时间戳呢?这将为您将来提供更大的灵活性。
现在您的问题,我怀疑这是由于您对 NSDateFormatter 的配置造成的,您将其保存为一种格式并尝试使用不同的格式将其转换为日期。使格式相同并重试。如果您想以不同于存储的方式显示它,您可能需要使用存储的格式将其转换为 NSDate,然后再次使用另一个日期格式化程序将其转换为您想要的字符串格式。
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
正如 Narayana 建议的那样,您需要以与存储的格式相同的格式检索日期。按如下方式检索: -
希望对您有帮助。
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
Hope it helps you.
尝试将其格式化为
dd-MM-yyyy hh:mm:ss
您编写了
dd-MM-yyyy hh:MM:SS
a,其中hh:MM:SS
中的MM
给出了此格式无法识别的月份写大写的 SS 几秒钟是没有意义的希望你能理解。
Try to format it to
dd-MM-yyyy hh:mm:ss
a.You wrote
dd-MM-yyyy hh:MM:SS
a whereMM
inhh:MM:SS
gives month which is unrecognized in this format and there is no point writing upercase SS for secondsHope you understand it.