В своем приложении я использую JSON для получения записей MySQL.

Одно из полей относится к типу даты (2014-03-08), и я хочу показать его в ячейке tableView, но преобразовано в четыре строки:

  1. День недели (например, понедельник).
  2. Месяц (например, март).
  3. Год (например, 2014).
  4. Время (например, 11:00).

Поля MySQL:

  1. 2014-03-10

  2. 2014-03-25

  3. 2014-12-01

Я использую следующий код, чтобы делать то, что мне нужно:

  NSString *fecha = [[categorias objectAtIndex:indexPath.row] objectForKey:@"dia"];//fecha is the date from MySQL.


    //convertir fecha


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yy-mm-dd"];
    NSDate *date = [formatter dateFromString:fecha];



    NSDateFormatter *weekDay = [[NSDateFormatter alloc] init] ;
    [weekDay setDateFormat:@"EEEE"]; //day of the week

    NSDateFormatter *calMonth =[[NSDateFormatter alloc] init] ;
    [calMonth setDateFormat:@"MMMM"];//month in text format(March)

    NSDateFormatter *calYear = [[NSDateFormatter alloc] init];
    [calYear setDateFormat:@"YYYY"];//year

    NSDateFormatter *calDay = [[NSDateFormatter alloc] init];
    [calDay setDateFormat:@"dd"]; //day

    cell.diaLabel.text = [calDay stringFromDate:date] ;//label to show day


    cell.diaSemanaLabel.text = [weekDay stringFromDate:date];//label to show weekDay 

    cell.mesLabel.text = [calMonth stringFromDate:date];//label to show month

    cell.anoLabel.text = [calYear stringFromDate:date];//label to show year

    NSString *hora = [[categorias objectAtIndex:indexPath.row] objectForKey:@"hora"];//hora  is the time field from MySQL

    cell.horaLabel.text = hora; //label to show the time
    //fin convertir fecha

Все строки отображаются отлично, кроме строки месяца, она всегда показывает «Январь».

Любая помощь приветствуется.

0
mvasco 9 Мар 2014 в 09:51

2 ответа

Лучший ответ

Строка средства форматирования для анализа даты должна быть @"yyyy-MM-dd", а не @"yy-mm-dd". MM - месяц. mm для минуты. Если вы регистрируете значение NSDate (или исследуете его в отладчике), вы можете подтвердить, что ваша текущая строка формата не извлекает дату, которую вы думаете.

2
Rob 9 Мар 2014 в 10:00

Попробуй это

NSString *fecha = [[categorias objectAtIndex:indexPath.row] objectForKey:@"dia"];//fecha is the date from MySQL.


    //convertir fecha


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:fecha];



    NSDateFormatter *weekDay = [[NSDateFormatter alloc] init] ;
    [weekDay setDateFormat:@"EEEE"]; //day of the week

    NSDateFormatter *calMonth =[[NSDateFormatter alloc] init] ;
    [calMonth setDateFormat:@"MMMM"];//month in text format(March)

    NSDateFormatter *calYear = [[NSDateFormatter alloc] init];
    [calYear setDateFormat:@"YYYY"];//year

    NSDateFormatter *calDay = [[NSDateFormatter alloc] init];
    [calDay setDateFormat:@"dd"]; //day

    cell.diaLabel.text = [calDay stringFromDate:date] ;//label to show day


    cell.diaSemanaLabel.text = [weekDay stringFromDate:date];//label to show weekDay 

    cell.mesLabel.text = [calMonth stringFromDate:date];//label to show month

    cell.anoLabel.text = [calYear stringFromDate:date];//label to show year

    NSString *hora = [[categorias objectAtIndex:indexPath.row] objectForKey:@"hora"];//hora  is the time field from MySQL

    cell.horaLabel.text = hora; //label to show the time
    //fin convertir fecha
1
Rashad 9 Мар 2014 в 10:02