将MySQL日期字段转换为字符串

时间:2014-03-09 05:51:51

标签: ios nsdateformatter

在我的应用程序中,我使用JSON来检索MySQL记录。

其中一个字段来自日期类型(2014-03-08),我想在tableView单元格上显示它,但转换为四个字符串:

  1. 星期几(例如星期一)。
  2. 月(例如三月)。
  3. 年(例如2014年)。
  4. 时间(例如11:00)。
  5. MySQL字段是:

    1. 2014年3月10日

    2. 2014年3月25日

    3. 2014年12月1日

    4. 我使用以下代码来做我需要的事情:

        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
      

      所有字符串都完美显示,除了月份字符串外,它始终显示' 1月'。

      欢迎任何帮助。

2 个答案:

答案 0 :(得分:2)

用于解析日期的格式化程序字符串应为@"yyyy-MM-dd",而不是@"yy-mm-dd"MM是月份。 mm是分钟。如果您记录NSDate值(或在调试器中检查它),则可以确认当前格式字符串未检索到您认为的日期。

答案 1 :(得分:1)

试试这个

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