-1

This is a really basic question, I guess, but I can't seem to find the answer in any of my books.

Suppose I have a date, created like this:

NSDate *today = [NSDate date];

But I want to change the time part of 'today' to '00:00:00'. How can I do this?

---- added ----

I tried this:

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:11];
    [components setMonth:5]; 
    [components setYear:2013];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *startDate = [calendar dateFromComponents:components];
    NSLog(@"Current date: %@", startDate);

But NSLog prints out this:

Current date: 2013-05-11 04:00:00 +0000

I'm guessing this has something to do with my NY timezone?

-- ANSWER --

Here is the answer for anyone who is looking for it. The trick to eliminate the 04:00:00 from the time is to set the timezone to GMT:

[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

-- REVISED ANSWER --

But you might actually want the local time -- for example, if you need to find a record in core data by date, and the stored date is in local time -- so you might not want to set the timezone to GMT, after all.

John
  • 2,121
  • 1
  • 23
  • 38
  • 2
    NSDateFormatter you need – nsgulliver May 14 '13 at 15:06
  • 1
    I can't see how to use NSDateFormatter for this... I was thinking NSDateComponents, but haven't been able to figure it out. – John May 14 '13 at 15:10
  • 1
    possible duplicate of [How can I get an NSDate object for today at midnight?](http://stackoverflow.com/questions/9040319/how-can-i-get-an-nsdate-object-for-today-at-midnight) or [How do I create the current date (or any date) as an NSDate without hours, minutes and seconds?](http://stackoverflow.com/questions/4832536/how-do-i-create-the-current-date-or-any-date-as-an-nsdate-without-hours-minut/4832602#4832602) – Matthias Bauch May 14 '13 at 15:12
  • 1
    Use NSCalendar. You can convert the NSDate into components, edit them, and convert it back. – borrrden May 14 '13 at 15:13

2 Answers2

5

If you want to set the components of a date, you want to look at NSCalendar's methods for dealing with date components:

NSDate *today = [NSDate date];

// Get the year, month, day from the date
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];

// Set the hour, minute, second to be zero
components.hour = 0;
components.minute = 0;
components.second = 0;

// Create the date
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
brush51
  • 5,691
  • 6
  • 39
  • 73
Simon
  • 25,468
  • 44
  • 152
  • 266
  • Edited original question to respond to this answer... – John May 14 '13 at 15:19
  • Okay, so this answered my question, although it took a while for me to figure out 1) why the time was coming out like '04:00:00', even after setting the hour component to 0, and 2) why this was a good thing. I need this date to do a core data query, and *thought* I needed a time of 00:00:00, but since the time in the database was local time, not GMT, I really did need a timezone-adjusted midnight, so to speak. I don't understand why this question generated so many negative waves... it really is NOT a basic question which the previously-asked questions don't really address. Anyway, thanks! – John May 14 '13 at 16:10
3

I think what you're looking for is NSDateComponents. From the docs:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];
NSCalendar *gregorian = [[NSCalendar alloc]
    initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];

Instead of setDay: etc. use setHour:. Hope that helps.

Maarten
  • 1,873
  • 1
  • 13
  • 16