Default value for Date Atttribute in Core Data application

  • Hello,

    I have a Core Data application that has a date attribute for one of
    the entities. I'd like to set the default value for this attribute to
    be the current date and time that the data was created. I found that I
    can use a default value of 'now' or 'today' and it will populate the
    attribute with the date the data was created, but not the time.

    Is there a default value I can use that provides both the date and
    time the data was created to be used in the default value for a date
    attribute?

    Thanks,

    Lee

    --

    http://www.leegillen.com
  • Lee,
    I believe that you need to implement awakeFromInsert

    HTH
    Robert

    On 10.8.2006, at 15:18, Lee Gillen wrote:

    > Hello,
    >
    > I have a Core Data application that has a date attribute for one of
    > the entities. I'd like to set the default value for this attribute to
    > be the current date and time that the data was created. I found that I
    > can use a default value of 'now' or 'today' and it will populate the
    > attribute with the date the data was created, but not the time.
    >
    > Is there a default value I can use that provides both the date and
    > time the data was created to be used in the default value for a date
    > attribute?
    >
    > Thanks,
    >
    > Lee
    >
    > --
    >
    > http://www.leegillen.com
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<robertcerny...>
    >
    > This email sent to <robertcerny...>
  • On Aug 10, 2006, at 9:18 AM, Lee Gillen wrote:

    > I have a Core Data application that has a date attribute for one of
    > the entities. I'd like to set the default value for this attribute to
    > be the current date and time that the data was created. I found that I
    > can use a default value of 'now' or 'today' and it will populate the
    > attribute with the date the data was created, but not the time.

    I couldn't do this with a default value -- ran into the same problem,
    "TODAY" produces a time of 12 noon.

    What I did was set the time to [NSDate date] in code after creating
    the object.

    Bill Coleman, AA4LR, PP-ASEL        Mail: <aa4lr...>
    Quote: "We invented personal computing."
                -- Bill Gates @ TechNet / MSDN 2003
  • Yes, using "now" and "today" give the same result. Both produce an
    NSDate with the date set to the current date and the time set to 12
    pm for the the current time zone. I think that result is appropriate
    for "today" but it is not for "now". "Now" should designate the
    current date *and* time. IMHO at least.

    Robert Cerny correctly stated that using awakeFromInsert is the
    appropriate place to use [NSDate date] as you suggested. I ended up
    with the following in my NSManagedObject subclass.

    - (void)awakeFromInsert
    {
    [super awakeFromInsert];
    [self setValue:[NSDate date] forKey:@"dateCreated"];
    }

    While this is very simple, if "Now" included the current date and
    time I would not have had to subclass and add this method.

    Thank you Bill and Robert for your help.

    Lee

    On Aug 12, 2006, at 8/12/06 8:21 PM, Bill Coleman wrote:

    >
    > On Aug 10, 2006, at 9:18 AM, Lee Gillen wrote:
    >
    >> I have a Core Data application that has a date attribute for one of
    >> the entities. I'd like to set the default value for this attribute to
    >> be the current date and time that the data was created. I found
    >> that I
    >> can use a default value of 'now' or 'today' and it will populate the
    >> attribute with the date the data was created, but not the time.
    >
    > I couldn't do this with a default value -- ran into the same
    > problem, "TODAY" produces a time of 12 noon.
    >
    > What I did was set the time to [NSDate date] in code after creating
    > the object.
    >
    > Bill Coleman, AA4LR, PP-ASEL        Mail: <aa4lr...>
    > Quote: "We invented personal computing."
    > -- Bill Gates @ TechNet / MSDN 2003
    >
    >
previous month august 2006 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today