首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否在xcode中将日期转换为MM/dd/yyyy格式?

是否在xcode中将日期转换为MM/dd/yyyy格式?
EN

Stack Overflow用户
提问于 2012-10-30 20:26:23
回答 9查看 46K关注 0票数 16

我有一个UIDatePicker,我要在yyyy-MM-dd format.Now中从它中获取选定的日期,我想将它转换为MM/dd/yyyy格式..How可以吗?

NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];

[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(@"%@",dateString2);

我得到了2012年10月30日,但我想要2012年10月30日。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2012-10-30 20:29:26

查看每一行的说明或介绍

NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);
票数 45
EN

Stack Overflow用户

发布于 2012-10-30 20:28:40

df.dateFormat = @"MM/dd/yyyy";

票数 1
EN

Stack Overflow用户

发布于 2012-10-30 20:30:40

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13138957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档