【发布时间】:2017-01-06 12:42:14
【问题描述】:
我想将我的输入字符串转换为特定的NSDate 格式,
这是我尝试过的代码。
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat1 setDateFormat:@"yyyy/MMM/dd"];
NSLog(@"maxUpcomingDateString %@",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(@"max1 date upcoming %@",result);
NSLog(@"max2 date upcoming %@", resultDate);
我的日志输出显示:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
我想要2017/Jan/10 形式的输出日期。当我尝试记录字符串时,它会显示正确的输出,但是当我将其转换回NSDate 时,它会显示为空。
【问题讨论】:
-
为什么要将它转换回 NSDate 对象?您已经拥有该日期字符串的 NSDate 对象。
-
d变量的意义何在?它不起作用,而且似乎无论如何都没有使用。
标签: ios objective-c nsdate