简体   繁体   English

如何使用nsdateformatter将nsdate转换为另一种格式

[英]how to convert nsdate to another format using nsdateformatter

I have date string in this format Feb 23, 2014 coming from web services, I want to convert it to this format 02-23-2014, I am trying a lot but all the time nsstring return me null value 我有2014年2月23日这种格式的日期字符串来自网络服务,我想将其转换为这种格式2014年2月23日,我正在尝试很多,但一直以来nsstring返回我的null值

NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateStyle:NSDateFormatterMediumStyle];
[DateFormatter setDateFormat:@"MM-dd-yyyy"];

// date formatter two

NSDate *dateVal = (NSDate *)bookDetail.classdate;
NSLog(@"date %@",dateVal);

NSString *dateTwo = [DateFormatter stringFromDate:dateVal];
NSLog(@"new date string %@",dateTwo);
NSString *dateString = @"Feb 23, 2014";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM