Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newdev

macrumors newbie
Original poster
Aug 5, 2008
4
0
I need help with NSCalendarDate....

How do I convert the following functionality to iPhone app?

PHP:
	NSCalendarDate *myDate = [[NSCalendarDate calendarDate]
								dateByAddingYears:0
								months:0
								days:0
								hours:-[[NSCalendarDate calendarDate] hourOfDay]
								minutes:-[[NSCalendarDate calendarDate] minuteOfHour]
								seconds:-[[NSCalendarDate calendarDate] secondOfMinute]];
	
	int hours = 12;
	int minutes = floor(hours * 60);
	
	return [myDate dateByAddingYears:0 months:0 days:0 hours:hours minutes:minutes seconds:0];

any ideas?
 

grimjim

macrumors member
May 24, 2003
75
0
Well, NSCalendarDate has been deprecated in favour of NSDate, so I suggest you look up the NSDate class reference in the documentation, and see how much of it has changed. You might also want to read 'Date and Time Programming Guide for Cocoa' which is also available on the documentation. I suspect that you won't need to change very much, just start using NSDate objects instead of NSCalendarDate.
 

jguru

macrumors member
Apr 10, 2008
43
0
I have been struggling with the same issue. I will let u know once I find a solution.. there isn't a 1-1 replacement of all the functionality of NSCalendarDate in NSDate .. :(
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
You need a combination of NSDate, NSDateComponents, and NSCalendar. It's pretty easy once you figure them out.
 

jguru

macrumors member
Apr 10, 2008
43
0
You need a combination of NSDate, NSDateComponents, and NSCalendar. It's pretty easy once you figure them out.

do you have any small example that can show how to replace the above code? it will be of great help :)

thanks
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I'm trying to work out what the code is trying to do. What is the output date?

It seems to take the current date and time, delete the hours, minutes, seconds (so getting back to midnight?). Then it adds 12 hours and 12*60 minutes so gets to the start of tomorrow?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Assuming my interpretation is correct:

Code:
NSDate *now = [[NSDate alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:now];

[comps setHour:-[comps hour]];
[comps setMinute:-[comps minute]];
[comps setSecond:-[comps second]];

NSDate *startOfToday = [cal dateByAddingComponents:comps toDate:now options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[comps setHour:24];
[comps setMinute:0];
[comps setSecond:0];

NSDate *startOfTomorrow = [cal dateByAddingComponents:comps toDate: startOfToday options:0];

Note this code is untested. I've not even compiled it: I just typed it straight into here. Assume there are typos and errors.

This all seems pretty simple and clear. Have you tried reading the documentation?
 

jguru

macrumors member
Apr 10, 2008
43
0
^ thanks.. i am not sure what newdev was trying to do but your example helps me a lot.. :) cheers
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.