Problem with Nsdate

Asked

Viewed 526 times

0

I have an Imobject class, in it I have an Nsdate *Startdate field. I fill this field with a database return:

 (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 print this field it shows me 3 more hours, in case 2014-05-28 11:00:00:00.

I think it might be some kind of time zone problem, but I don’t know how to fix it. My project is with Localizations Portuguese. Does anyone know how to solve or have any idea what it might be?

2 answers

1

If you want to print an object of type NSDate, you must use a NSDateFormatter. In Formatter you can set the time zone (time zone) which 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);

1


It’s unclear how you manipulate the bank, and in particular, the date field. It would be necessary to see the writing and reading code and know what technology is used. Anyway, to format an object of type NSDate an object is used NSDateFormatter, example:

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 was used a NSDateFormatter to format the textual representation of the object date, forcing 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 string, since sqlite does not have a specific type for dates, and how many more need to manipulate the dates for viewing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.