How do I get the day of the week with NSCalendar? - iphone

I want to get the day of the week, preferable in an integer from 1 to 7... is this possible?
This is what I currently have to get the current hour, minute and second....
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[NSDate date]];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

You can use this:
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:myDate];
NSInteger weekday = [weekdayComponents weekday];
Or to integrate with the code you already have:
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];
NSInteger weekday = [dateComponents weekday];
That will get you values 1 .. 7 where 1 is Sunday.
Hope that helps.

Related

display the difference in Days, Hours, Minutes and Seconds with it then updating in realtime?

I am a noob...
I have
QuitTime.text = quitDate;
Which is a date in the past...
How can I display the difference in Days, Hours, Minutes and Seconds with it then updating in realtime?
You can use NSDateComponents
NSDate *currentDate = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:quitDate
toDate:currentDate options:0];
NSLog(#"Component Month:%d",[component month]);
NSLog(#"Component Day:%d",[component day]);
NSLog(#"Component Hour:%d",[component hour]);
NSLog(#"Component Mins:%d",[component minute]);
Reference:Difference between two NSDate objects -- Result also a NSDate

Forming a predicate to filter between dates

Say I have two dates:
minDate:2012-08-29 12:22:17 +0000
maxDate:2011-12-01 18:14:38 +0000
I have a Core Data object called MBDate which has an NSDate property. I want to get all MBDate's with days including and in between the days above. So it should disregard the hours, seconds, and minutes, and just get all days in between and including 8/29 - 12/01.
Something like:
predicate = [NSPredicate predicateWithFormat:#"date < %# AND date > %#", maxDate, minDate];
But I'm not sure how I would tell it to disregard the hours and minutes and just look at the days. Any ideas?
You could use NSDateComponents to set a 'low' and 'high' date and use these in your predicate.
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = [NSTimeZone timeZoneWithAbbreviation"#"UTC"];
NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:minDate];
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
NSDate *lowDate = [gregorian dateFromComponents:dateComponents];
dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:maxDate];
[dateComponents setHour:23];
[dateComponents setMinute:59];
[dateComponents setSecond:59];
NSDate *highDate = [gregorian dateFromComponents:dateComponents];

obj-C, why does this give me yesterdays date?

I've been struggling with my function to return todays date, at as close to zero seconds, minutes and hours as possible. So I'm able to re-use the same date with various transactions.
However, I've now discovered that my function returns yesterdays date?
+ (NSDate *)makeAbsoluteNSDate:(NSDate*)datSource {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:
NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:datSource];
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
NSDate *today = [calendar dateFromComponents:dateComponents];
[calendar release];
return today;
}
If you're trying to get today's or yesterday's midnight in your time zone, you should get NSDate that points to adequate time in GMT. In my case -2 hours as my timezone is +0200, so for midnight of 2011-08-26 I'll get 2011-08-25 22:00.
You have to make sure that you're setting the right timezone for the NSCalendar, which is [NSTimeZone systemTimeZone].
My routine for getting the midnight in my time zone is:
+ (NSDate *)midnightOfDate:(NSDate *)date {
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
[components setTimeZone:[cal timeZone]];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
So, after a long discussion in comments, here's a midday function:
+ (NSDate *)middayOfDate:(NSDate *)date {
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
[components setTimeZone:[cal timeZone]];
[components setHour:12];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
Use below function to get date for yesterday :
- (NSDate *)getDate:(NSDate *)date days:(NSInteger)days hours:(NSInteger)hours mins:(NSInteger)mins seconds:(NSInteger)seconds
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
days = (days*24) + hours;
[components setHour:days];
[components setMinute:mins];
[components setSecond:seconds];
NSDate *returnDate = [cal dateByAddingComponents:components toDate:date options:0];
return returnDate;
}
To get yesterday date pass date = [NSDate date], day = -1, hours = 0 and mins = 0 as parameters in method calling.
All you need is [NSDate date] for the current date & time.

Get the weekday from a selected day and not from the current day

I've seen many ways to get the weekday, but none of them was from a selected day.
NSDate * selectedDate = _selectedDate //From Params
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];
NSInteger weekday = [components weekday];
How and where should I insert my selected Date?
Since [components setDay:(INTEGER REQUIRED)]
is it a way to convert the selected day to the current day.. anyone got a clue on this?
Thank's
HaveYou tried?
NSDateComponents *components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:selectedDate];
NSDateComponents *components =
[cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:selectedDate];

NSDate get a specific day of the week?

I hope this is just a learning curve I'm going through.
I create an arbitrary date. I want to find the Saturday following the third Friday.
NSLog(#"Date: %#", [date description]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:#"EST"];
[calendar setTimeZone:zone];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
[components setWeekday:6]; // 1 = Sunday ... 7 = Saturday
[components setWeekdayOrdinal:3]; // 3rd Friday
resultDate = [calendar dateFromComponents:components];
NSLog(#"Date: %#", [resultDate description]);
And the output:
Date: 2010-11-01 05:00:00 GMT
Date: 2010-11-01 05:00:00 GMT
Why? How can I fix this?
// Sunday = 1, Saturday = 7
int weekday = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate: date] weekday];
Change the components to:
(NSWeekdayCalendarUnit | NSYearCalendarUnit)
This works:
NSDate *today = [[NSDate alloc] init];
NSLog([today description]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:today];
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday])];
[componentsToSubtract setWeekdayOrdinal:([weekdayComponents weekday] <= 6) ? 3 : 4];
//[componentsToSubtract setWeekdayOrdinal:3]; //old way - not perfect
NSDate *saturday = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
NSDateComponents *components =
[gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: saturday];
saturday = [gregorian dateFromComponents:components];
NSLog([saturday description]);
Hope this helps.