在我的应用程序中,我使用JSON检索MySQL记录。
其中一个字段来自日期类型(2014-03-08),我想在tableView单元格中显示它,但转换为四个字符串:
-
一周中的某一天(例如星期一)。
-
月份(例如3月)。
-
年份(如2014年)。
-
时间(例如11:00)。
MySQL字段包括:
-
2014-03-10
-
2014-03-25
-
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
所有字符串都完美显示,除了月份字符串,它始终显示“一月”。
欢迎任何帮助。