web-dev-qa-db-ja.com

MM / dd / yyyy形式の日付をxcodeに変換しますか?

私は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が必要です。

16
Honey

すべての行の説明または紹介を参照してください

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);
46
Paras Joshi
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy"];    
1

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

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

それがどのように終わるかを見るだけです:

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);
1
Hot Licks

元の受け入れられた答えは、いくつかの追加のことをします:

1)不要な2つのNSDateFormatterインスタンスを割り当てます。 NSDateFormatterは作成にコストがかかります。

2)自動リリースを使用した遅延リリースよりも、日付フォーマッターを使い終わったら明示的にリリースすることをお勧めします。

//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];
1
SayeedHussain
- (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;
}

楽しい

1
Frank
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);
0
mahesh chowdary

オプション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
0
Agisight