gpt4 book ai didi

iphone - NSDateFormatter stringFromDate, dateFromString 都返回 nil

转载 作者:行者123 更新时间:2023-11-28 18:24:02 25 4
gpt4 key购买 nike

您好,我正在尝试格式化我的 NSString。但它不起作用。我尝试了在 stackoverflow 中得到的所有答案,但对我没有任何作用。

NSLog(@"log 1: %@ %@", [dealDetailsDict objectForKey:@"DealDateViewed"], [[dealDetailsDict objectForKey:@"DealDateViewed"] class]);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"log 2: %@ %@", [df stringFromDate:[dealDetailsDict objectForKey:@"DealDateViewed"]], [df dateFromString:[dealDetailsDict objectForKey:@"DealDateViewed"]]);

我不知道这里有什么问题,stringFromDatedateFromString 都返回nil。这是我的 NSLog

log 1: 2013-01-09 10:02:45 +0000 __NSCFString
log 2: (null) (null)

我想显示像 31-12-2010 这样的日期。所以我设置了,

[df setDateFormat:@"dd-MM-yyyy"];

我的问题从这里开始。

最佳答案

使用以下日期格式功能...

-(NSString*) Dateformate:(NSString*)str
{
NSDateFormatter *dfrt =[[NSDateFormatter alloc] init];
[dfrt setDateFormat:@"yyyy-MM-dd h:mm:ss"];
NSDate *presentdate = [dfrt dateFromString:str];
[dfrt setDateFormat:@"MMMM"];
NSString *fullMonth = [dfrt stringFromDate:presentdate];

[dfrt setDateFormat:@"yyyy"];
NSString *yearStr = [dfrt stringFromDate:presentdate];
// NSLog(@"month %@",presentdate);

[dfrt setFormatterBehavior:NSDateFormatterBehavior10_4];
[dfrt setDateFormat:@"d"];
int date_day = [[dfrt stringFromDate:presentdate] intValue];
NSString *suffix_string = @"|st|nd|rd|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|st|nd|rd|th|th|th|th|th|th|th|st";
NSArray *suffixes = [suffix_string componentsSeparatedByString: @"|"];
NSString *suffix = [suffixes objectAtIndex:date_day];

NSString *fullDateFormat = [NSString stringWithFormat:@"%@ %d%@ %@",fullMonth,date_day,suffix,yearStr];

// NSLog(@"fullDateFormat %@",fullDateFormat);

return fullDateFormat;

}

关于iphone - NSDateFormatter stringFromDate, dateFromString 都返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com