Ios 将MySQL日期字段转换为字符串
在我的应用程序中,我使用JSON检索MySQL记录 其中一个字段是from date类型(2014-03-08),我想在tableView单元格中显示它,但已转换为四个字符串:Ios 将MySQL日期字段转换为字符串,ios,nsdateformatter,Ios,Nsdateformatter,在我的应用程序中,我使用JSON检索MySQL记录 其中一个字段是from date类型(2014-03-08),我想在tableView单元格中显示它,但已转换为四个字符串: 一周中的某一天(如星期一) 月份(如三月) 年份(如2014年) 时间(如11:00) MySQL字段包括: 2014-03-10 2014-03-25 2014-12-01 我正在使用以下代码执行我需要的操作: NSString *fecha = [[categorias objectAtIndex:indexPa
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
所有字符串都显示得很完美,除了月份字符串,它总是显示“一月”
欢迎提供任何帮助。用于解析日期的格式化程序字符串应该是
@“yyy-MM-dd”
,而不是@“yy-MM-dd”
。MM
表示月份。mm
表示分钟。如果记录NSDate
值(或在调试器中检查它),可以确认当前格式字符串没有检索到您认为是的日期。解析日期的格式化程序字符串应该是@“yyyy-MM-dd”
,而不是@“yy-MM-dd”
。MM
表示月份。mm
表示分钟。如果记录NSDate
值(或在调试器中检查它),可以确认当前格式字符串没有检索到您认为是的日期。解析日期的格式化程序字符串应该是@“yyyy-MM-dd”
,而不是@“yy-MM-dd”
。MM
表示月份。mm
表示分钟。如果记录NSDate
值(或在调试器中检查它),可以确认当前格式字符串没有检索到您认为是的日期。解析日期的格式化程序字符串应该是@“yyyy-MM-dd”
,而不是@“yy-MM-dd”
。MM
表示月份。mm
表示分钟。如果您记录NSDate
值(或在调试器中检查它),您可以确认当前格式字符串没有检索到您认为是的日期。尝试此操作
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
试试这个
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
试试这个
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
试试这个
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
谢谢你的回答。谢谢你的回答。谢谢你的回答。谢谢你的回答。谢谢你的回答。