Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:07:12+00:00 2026-05-27T19:07:12+00:00

I have a doubt whether NSDate can be directly stored or read from the

  • 0

I have a doubt whether NSDate can be directly stored or read from the database using FMDatabase or not.

As per my research, FMDatabase reads date from record expecting that tuple to be a double value. FMDatabase converts that double value to date in this method:

- (NSDate*) dateForColumn:(NSString*)columnName; {
    int columnIdx = [self columnIndexForName:columnName];

    if (columnIdx == -1) {
        return nil;
    }

    return [NSDate dateWithTimeIntervalSince1970:[self doubleForColumn:columnName]];
}

The main problem with this approach is, it would be difficult for the person who is manually entering data in the database so that it would be understandable by FMDatabase in the code. So someone cannot simply enter the date in double value in GUI. One approach is to use one of these functions

date(timestring, modifier, modifier, ...)
time(timestring, modifier, modifier, ...)
datetime(timestring, modifier, modifier, ...)
julianday(timestring, modifier, modifier, ...)
strftime(format, timestring, modifier, modifier, ...)

But the overhead here is again to run a query each time for entering a record using insert operation similar to this:

Compute the current date.
SELECT date('now');

Compute the last day of the current month.
SELECT date('now','start of month','+1 month','-1 day');

I would like the date to be stored in the database in following format:

TEXT as ISO8601 strings ("YYYY-MM-DD")

And store it as Date format in the database so that I can:

  1. Leverage the date comparison facility in queries.
  2. Make FMDatabase understandable to read and write this date format.

Is there any direct way where FMDatabase can interact with this kind of date format? Or is this a limitation of FMDatabase and I will have to extend the FMDatabase class to handle reading and writing back of NSDate to the sqlite date format?

Thanks

Edit: One of the author / moderator of FMDatabase has to say this about it: http://code.google.com/p/flycode/issues/detail?id=16#c1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:07:12+00:00Added an answer on May 27, 2026 at 7:07 pm

    Reading date from database which is entered in the format YYYY-MM-DD.

    I have extended the FMResultSet class to include the following additional methods:

    FMResultSet+Addtion.h file:

    typedef enum
    {
        eText,
        eReal,
        eInteger
    }EDateAndTimeDataType;
    // Reference: http://www.sqlite.org/datatype3.html
    
    typedef enum
    {
        eDateString    // YYYY-MM-DD format
        // Rest is un implemented
    }EDateAndTimeFunctionType;
    // Refernce: http://www.sqlite.org/lang_datefunc.html
    
    @interface FMResultSet (Additions)
    
    -(NSDate*)dateForColumn:(NSString *)columnName withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType;
    -(NSDate*)dateForColumnIndex:(int)columnIdx withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType;
    
    @end
    

    FMResultSet+Addition.m file:

    @implementation FMResultSet (Additions)
    
    -(NSDate*)dateForColumn:(NSString *)columnName withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType
    {
        return [self dateForColumnIndex:[self columnIndexForName:columnName]
                           withDataType:inDateDataType
                           functionType:inFunctionType];
    }
    
    -(NSDate*)dateForColumnIndex:(int)columnIdx withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType
    {
        NSAssert (eText==inDateDataType, @"Only Text type is implemented for now");
        NSAssert (eDateString==inFunctionType, @"Only date() function type is implemented for now");
    
        NSDate *theDate = nil;
        NSString *dateString = [self stringForColumnIndex:columnIdx];
        if (nil!=dateString)
        {
            theDate = [Utility getDateFromString:dateString
                                  withDateFormat:@"yyyy-MM-dd"];
            NSLog(@"Date from DB: %@", theDate);
        }
        return theDate;
    }
    
    @end
    

    Utility:

    +(NSDate *)getDateFromString:(NSString *)inString withDateFormat:(NSString*)inDateFormat
    {
        NSDate *date =nil;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setDateFormat:inDateFormat];
    
        NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        [dateFormatter setLocale:enUSPOSIXLocale];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [enUSPOSIXLocale release];
    
        date = [dateFormatter dateFromString:inString];
        [dateFormatter release];
        return date;
    }
    

    **Edit: Though this works for queries like “select * from table_name” it fails in queries comparing for dates. Need solutions for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have doubt whether we can do the following or not. Suppose I have
I have doubt whether we can achieve multiple inheritence using 'module' concept or Is
I have a doubt in layouts.I have designed one layout using group layout using
I have one doubt in string . How can we acess the memory of
I have a doubt regarding the backlog value in listen system call. From man
I have used MKReverseGeocoder from iPhone sdk for my application without using mapview of
alt text http://img8.imageshack.us/img8/8558/classdiagram.png Short description: I have a doubt whether it's normal that AbstractCrudDaoImpl
i have a doubt that whether javascript expression is better or regularexpression is better.
I have been using Hibernate/Spring. I have a small doubt. If we get 1000
I have a doubt, I will appreciate if you can clear it . COOKIES

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.