Home > DeveloperSection > Forums > Objective-C NSDate type and SQLite's DATETIME type?
Tarun Kumar

Total Post:214

Posted on    September-21-2015 11:50 PM

 iPhone Database  iPhone  Ios  Sqlite  Sqlite3  Objective C 

 1 Reply(s)
 915  View(s)
Rate this:

In objective-C I was trying to map a sqlite3 table, its one of the attribute is 'completed_at' and its type defined as DATETIME.

I want to create a property on my objective-C class (which inherits from NSObject) that will map well to the 'completed_at' attribute.

Objective-C has an NSDate type but I'm not sure if that will map directly?

Felix Pickles
Felix Pickles

Total Post:24

Posted on    September-22-2015 2:47 AM

The formatter is important if you are trying to effect the presentation but if you use if for internal storage, you are defining a string which can defeat the DB-engine's ability to use the value for computation, comparison, sorting, etc. Also, if you are going to have different clients inserting the date value into the DB you would have to write conversion functions everywhere. I used the following and it worked as expected (schema's column defined as DATETIME): 

expireDate = [NSDate dateWithTimeIntervalSinceNow: sqlite3_column_double(queryStmt, 5)];


I inserted into the SQLITE3 db with the Firefox add-on as "4/12/2010" here in Central time zone. Viewing the value of ' expireDate' in XCode-debugger displayed as:

2015-09-12 13:19:48 -0500

Sure enough, that is the correct time.

Also, to insert into the SQLITE DB you will put the value [NSDate date]

Don't want to miss updates? Please click the below button!

Follow MindStick