题
我有一个像素的世代odicetagcode,然后我想将其转换为此格式为NSString
第二次日期是我从系统日期获得的日期。该日期可以在05/09/2015
Calender上很好地放置。但我的字符串是第一个。我想转换第一个日期,就像作为我从系统日期的日期格式一样。我怎样才能做到这一点。
请帮帮我。谢谢
解决方案
nsstring到nsdate
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
NSDateFormatter *otherFormatter = [[NSDateFormatter alloc] init];
[otherFormatter setDateFormat:@"yyyy-MM-dd"];
//[otherFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *resultString = [otherFormatter stringFromDate:dateFromString];
NSLog(@"%@", resultString);
.
然后,您可以将其转换回NSDate或保留为NSString。
看这个:点击这里
不隶属于 StackOverflow