16

我有一个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);

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

4

9 回答 9

45

请参阅每一行的说明或介绍

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);
于 2012-10-30T12:29:25.557 回答
1

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

于 2012-10-30T12:28:39.770 回答
1
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
于 2012-10-30T12:30:39.893 回答
1

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

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);
于 2012-10-30T13:00:44.293 回答
1
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy"];    
于 2014-08-13T04:20:01.087 回答
1

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

1)它分配了两个不需要的 NSDateFormatter 实例。NSDateFormatter 的创建成本很高。

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];
于 2014-09-20T12:34:20.807 回答
1
- (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;
}

享受

于 2015-07-23T07:33:31.370 回答
0
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);
于 2013-11-26T10:08:00.700 回答
0

我使用带有选项 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
于 2018-06-20T06:11:22.160 回答