来自NSString的NSDate给出null结果 [英] NSDate from NSString gives null result
本文介绍了来自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
。以下是我以特定格式检索日期的代码
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];
我应该如何检索或存储特定格式?我的crdInfo.swipeTime正在检索String属性......
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
推荐答案
由于Narayana建议您需要检索与您的格式相同的日期已存储。检索如下: -
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(@"The Date : %@",dt);
[reDateFormatter setDateFormat:@"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(@"%@",currentTime);
希望对您有帮助。
这篇关于来自NSString的NSDate给出null结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文