代码之家  ›  专栏  ›  技术社区  ›  mvasco

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

  •  0
  • mvasco  · 技术社区  · 11 年前

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

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

    1. 一周中的某一天(例如星期一)。
    2. 月份(例如3月)。
    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
    

    所有字符串都完美显示,除了月份字符串,它始终显示“一月”。

    欢迎任何帮助。

    2 回复  |  直到 11 年前
        1
  •  2
  •   Rob Md Fahim Faez Abir    11 年前

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

        2
  •  1
  •   Rashad    11 年前

    试试这个

    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