NSDate without time component

  • Hi everyone,

    I'm a newbie and have checked out the archives but I may not be
    searching for the right thing. Any help would be grateful.

    What I'm doing is using an NSDatePicker to return an instance of
    NSDate. All I want from this NSDate is the date, not the time
    component. Or if possible, when NSDatePicker returns the NSDate, the
    time is set to 00:00 UTC. Is this possible?

    Thanks
    JayJay
  • On Nov 27, 2005, at 12:33 AM, Jason Wiggins wrote:

    > I'm a newbie and have checked out the archives but I may not be
    > searching for the right thing. Any help would be grateful.
    >
    > What I'm doing is using an NSDatePicker to return an instance of
    > NSDate. All I want from this NSDate is the date, not the time
    > component. Or if possible, when NSDatePicker returns the NSDate,
    > the time is set to 00:00 UTC. Is this possible?

    I would have assumed that calling setDatePickerElements: and leaving
    out the ability the edit time values would normalize the time to
    something sane.

    If there's no way of telling the picker to normalize the time for
    you, you could try using NSDateComponents and NSCalendar:

    NSDate*    theDate = <your picker date>

    unsigned theUnitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
    NSDayCalendarUnit;
    NSCalendar* theCalendar = [NSCalendar currentCalendar];

    NSDateComponents* theComps = [theCalendar components:theUnitFlags
    fromDate:date];
    [theComps setHour:0];
    [theComps setMinute:0];
    [theComps setSecond:0];

    NSDate* theNormalizedDate = [theCalendar dateFromComponents:theComps];

    Note that you'll probably also have to play around with the time zone
    (see NSCalendar's setTimeZone:)

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
previous month november 2005 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        
Go to today
MindNode
MindNode offered a free license !