ios 在 xcode 中以 MM/dd/yyyy 格式转换日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13138957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:00:28  来源:igfitidea点击:

Convert date in MM/dd/yyyy format in xcode?

objective-ciosnsdateuidatepicker

提问by Honey

I have a UIDatePickerand I m getting the selected date from it in yyyy-MM-ddformat.Now I want to convert it to MM/dd/yyyyformat ..How can i do it ?

我有一个UIDatePicker,我从它的格式中获取选定的日期。现在yyyy-MM-dd我想将它转换为MM/dd/yyyy格式..我该怎么做?

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);

Im getting 2012-10-30 but I want 10/30/2012.

我收到 2012-10-30,但我想要 10/30/2012。

回答by Paras Joshi

See Instruction or introduction with every line

请参阅每行的说明或介绍

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);

回答by Eric

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

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

回答by Shamsudheen TK

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];

回答by Hot Licks

Just to see how it ends up:

只是为了看看它是如何结束的:

NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date];  // Don't use leading caps on variables
NSLog(@"%@", dateString);

回答by Brunetto Ferrarese

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy"];    

回答by SayeedHussain

The original accepted answer does a few things extra:

最初接受的答案做了一些额外的事情:

1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.

1) 它分配了两个不需要的 NSDateFormatter 实例。NSDateFormatter 创建起来很昂贵。

2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.

2) 最好在完成日期格式化程序后明确发布它们,而不是使用 autorelease 延迟发布。

//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];

//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 

//set its format as the required output format
[formatter setDateFormat:@"MM/dd/yyyy"];

//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];

//release formatter
[formatter release];

回答by Frank

- (void)showTime {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"HH:mm:ss"];
   // you label
   _time.text = [dateFormatter stringFromDate:now]; 
}

- (void)showDate {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"MM/dd/yyyy"];
   // you label
   _date.text = [dateFormatter stringFromDate:now];
}

- (void)viewDidLoad {
   [super viewDidLoad];
    self.showTime;
    self.showDate;
}

Enjoy

享受

回答by Agisight

I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:

我使用带有选项 NSISO8601DateFormatWithDashSeparatorInDate 的代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:@"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];

NSDate *d = [NSDate date]; // or set your date
NSLog(@"date in format %@",  [dateFormatter stringFromDate:d]); // date in format 2018-06-20

回答by mahesh chowdary

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);