Problem with NSDate

0

I have an IMObject class, in it I have an NSDate * StartDate field. I fill this field with a return of the database:

 (IMMutableArrayIMTable*)[databaseTemp retrieve: @"select * from table" withParams:nil forClass: [IMTable class]]

In the XCode Watch I see the value of the object as:

StartDate = (_NSDate *) 2014-05-28 08:00:00:00 BRT

However, if I give a print in this field it will show me 3 more hours in the case of 2014-05-28 11: 00: 00: 00 .

I think it might be some time zone problem, but I do not know how to solve it. My project is with Localizations English. Does anyone know how to solve it or do you have any idea what it might be?

    
asked by anonymous 27.05.2014 / 22:30

2 answers

1

It is not clear how you manipulate the bank, and in particular, the date field. It would be necessary to see the writing and reading code and to know which technology was used. However, to format an object of type NSDate you use a NSDateFormatter object, eg:

NSDate *date = [NSDate date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd EEEE"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"]];

NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"%@",dateString);

In the example a NSDateFormatter was used to format the text representation of the object date , forcing the locale to pt_BR (otherwise the device locale would be used).

I believe that in your case you need to create a formatter to save and read the dates in the database as a string, since sqlite does not have a specific type for dates, and how many more do you need to handle the dates for viewing. >     

27.05.2014 / 23:38
1

If you want to print an object of type NSDate , you must use a NSDateFormatter . In the formatter you can set the time zone ( time zone ) that should be used to convert the date to a string:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString = [dateFormat stringFromDate:StartDate];
NSLog(@"Data: %@", dateString);
    
27.05.2014 / 23:37