0

Stack Overflow で質問をスキャンしたにもかかわらず、NSDate を一生動作させることができないので、助けていただければ幸いです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSString *publishedText = [NSString stringWithFormat:@"%@", feedLocal.published];
cell.publishedLabel.text = publishedText;
    return cell;
}

私に文字列を与えます:

2013-05-08 18:09:37 +0000

2013 年 5 月 8 日午後 6 時 45 分

私は使用してみました:

    NSString *publishedText = [NSString stringWithFormat:@"%@", feedLocal.published];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [df dateFromString:publishedText];
    cell.publishedLabel.text = dateFromString;

しかし、それは機能せず、ポインターの型に互換性がないという警告が表示されます ( NSStringto NSDate_strong)。助けてくれてありがとう!

4

2 に答える 2

3

NSDateaを a に割り当てていNSString cell.publishedLabel.text = dateFromString;ます (cell.publishedLabel.textNSString.

編集

私はこのコードをテストしませんでしたが、iOSの日付フォーマット ガイドを確認してください。

したがって、文字列を解析してNSDateインスタンスを作成した後、次のコードを追加します。

編集 2 -- 完全なコード

NSString *publishedText = [NSString stringWithFormat:@"%@", feedLocal.published];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
NSDate *dateFromString = [df dateFromString:publishedText];

NSDateFormatter *secondDateFormatter= [[NSDateFormatter alloc] init];
[secondDateFormatter setDateStyle:NSDateFormatterLongStyle];
cell.publishedLabel.text = [secondDateFormatter stringFromDate:dateFromString];
于 2013-05-09T21:46:05.673 に答える
2

あなたが投稿したものから、それfeedLocal.publishedNSDate.

目標はこの日付を文字列に変換することなので、次のようなものが必要です。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM d, yyyy h:mma"]; // this matches your desired format
NSString *dateString = [df stringFromDate:feedLocal.published];
cell.publishedLabel.text = dateString;

あなたのアプリは世界中の人々が使用できるため、日付フォーマッタを次のように設定することをお勧めします。

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

特定の日付形式を設定する代わりに、これを行います。これにより、特定の国のユーザーだけでなく、アプリのすべてのユーザーに適切な日付と時刻が表示されます。

于 2013-05-09T22:27:01.993 に答える