Your formatter string for parsing the date should be @"yyyy-MM-dd"
, not @"yy-mm-dd"
. The MM
is for month. The mm
is for minute. If you log the NSDate
value (or examine it in the debugger), you can confirm that your current format string is not retrieving the date you think it is.
Converting MySQL date field to strings
-
11-06-2023 - |
Pergunta
In my app I am using JSON to retrieve MySQL records.
One of the fields is from date type ( 2014-03-08 ), and I want to show it on a tableView cell but converted to four strings:
- Day of the week (e.g. Monday).
- Month (e.g. March).
- Year (e.g. 2014).
- Time (e.g. 11:00).
The MySQL fields are:
2014-03-10
2014-03-25
2014-12-01
I am using the following code to do what I need:
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
All strings are shown perfectly, except the month string, it always shows 'January'.
Any help is welcome.
Solução
Outras dicas
Try this
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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow