1

私のコア データ エンティティには、UITableViewCell に出力できる日付フィールドがあり、次のように出力されます。

日にち

私がやりたいのは、この日付形式を変更して、もう少し人間が読めるようにすることです。

10月19日(金) 14:44

また

2012/10/19 14:44

Objective-Cでこれを行うにはどうすればよいですか? NSDate のフォーマッタ クラスがあると思いますが、プルされたコア データ文字列を NSDate に変換するにはどうすればよいですか?

4

2 に答える 2

4

コアデータオブジェクトを NSDate にキャストしようとしても、キャストを実行できませんでした。私はこれをやってしまった:

// Make a date formatter which corresponds exactly with the format of the date in core data
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

// Get the date from core data
NSString *dateStr = [[object valueForKey:@"date"] description];

// Parse from string to date
NSDate *date = [dateFormatter dateFromString:dateStr ];

// Set the date formatter to the format that I actually want
[dateFormatter setDateFormat:@"dd/MM/yy HH:mm"];

// Set the text on the date label
cell.dateLabel.text = [dateFormatter stringFromDate:date];
于 2012-10-25T10:43:41.943 に答える
1

この方法を試してください:

ロケールごとに調整する必要があるかもしれませんが、コードをコンパイルできるマシンがないため、テストしていません。

- (NSString *)dateAsString:(NSDate *)theDate {

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle]; 
[df setTimeStyle:NSDateFormatterNoStyle];

return  [df stringFromDate:theDate];
}
于 2012-10-19T15:26:47.563 に答える