Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

objective c - NSDateFormatter producing (null) SQLite and iOS 5.1

Maybe somebody can help explain why I am getting a null value when converting a string to a date. It all looks right but I'm obviously missing something here.

Some background:

This iPad app will be used in different countries and I will need to do a calculation on the date to see if 90 days have passed since a user last logged in.

I have a SQLite Database with a DateLastIn field set as TEXT

My Object has a DateLastIn property set as NSDate

When populating my record object I set up a NSDateFormatter as such..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];  // SQLite default date format

Then I read in the DateLastIn (Using FMDB wrapper for SQLite).

        // test SQLite as String
        NSString *testDate = [results stringForColumn:@"DateLastIn"];
        NSLog(@"DateLastIn straight from DB (string) shows %@", testDate);

Result: DateLastIn straight from DB (string) shows 2012-04-23 18:20:51

All is good so far. Next I test converting this to an NSDate object e.g

        NSDate *aDate = [[NSDate alloc] init];
        aDate = [formatter dateFromString:testDate];
        NSLog(@"Using formmater on string date results in: %@", aDate);

Result: Using formmater on string date results in: (null)

I have tried DATETIME in SQLite, I've tried using NSString in my object instead of NSDate and seem to be going around in circles.

Any help much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

NSDateFormatter uses the format patterns from the Unicode Technical Standard #35.

For the hour format, you need HH (Hour [0-23]) not hh (Hour [1-12]).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...