nscalendar

Get first day in a month from NSCalendar

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 06:05:25
问题 I have this subset of a method that needs to get day one of the current month. NSDate *today = [NSDate date]; // returns correctly 28 february 2013 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = 1; NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0]; dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000 , the

NSCalendar, why does setting the firstWeekday doesn't effect calculation outcome?

心已入冬 提交于 2019-12-18 06:06:24
问题 i need to calculate the weekday for a given date, however, depending on the calendar a week can star on Monday and somwhere on Sunday so i wanted to set it, to start on Monday, using [[NSCalendar currentCalendar] setFirstWeekday:2]; however, the calculation outcome is the same { [[NSCalendar currentCalendar] setFirstWeekday:1]; NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; NSInteger weekday =

How to find what day of the week for any given date using Cocoa

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 19:19:15
问题 I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. 回答1: I've been doing: NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [theDateFormatter setDateFormat:@"EEEE"]; NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]]; This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing

Compare two NSDates for same date/time [duplicate]

十年热恋 提交于 2019-12-17 18:37:47
问题 This question already has answers here : How to compare two dates in Objective-C (14 answers) Closed 6 years ago . Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:self]; NSDate* dateOnly = [calendar

NSCalendar first day of week

£可爱£侵袭症+ 提交于 2019-12-17 02:30:22
问题 Does anyone know if there is a way to set the first day of the week on a NSCalendar, or is there a calendar that already has Monday as the first day of the week, instead of Sunday. I'm currently working on an app that is based around a week's worth of work, and it needs to start on Monday, not Sunday. I can most likely do some work to work around this, but there will be a lot of corner cases. I'd prefer the platform do it for me. Thanks in advance Here's some the code that I'm using. it's

NSCalendar Find Saturday and Sunday of Current Month

家住魔仙堡 提交于 2019-12-13 21:03:08
问题 I am working on project where I want to integrate NSCalendar functionality in my app. Hence I googled and I found this solution. It give me all months in the year and also I can select particular date. that's fine. But I wanted to disable all saturdays and sundays of every month somehow I tried to do it but when I tap previous month or next month it changes the selection. I have gone through almost all stackoverflow questions But I didn't find proper way to do it. I request you to all Is

NSCalendar date error

為{幸葍}努か 提交于 2019-12-13 11:35:16
问题 I'm trying to use NSCalendar with NSIslamicCalendar identifier. But the day result is not good, her's my code: NSCalendar *calandar = [[NSCalendar alloc]initWithCalendarIdentifier:NSIslamicCalendar]; NSDateComponents *components = [calandar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; NSInteger theDay = [components day]; NSInteger theMonth = [components month]; NSInteger theYear = [components year]; [components setDay:theDay]; [components

[__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil

拟墨画扇 提交于 2019-12-13 10:38:34
问题 I am getting this error .How to solve it.Please suggest me. Crash Report [__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil I mean really, what do you think that operation is supposed to mean with a nil fromDate? An exception has been avoided for now. A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil. Here is the backtrace where this occurred this time (some

How to get number of weeks by month in year?

独自空忆成欢 提交于 2019-12-13 09:55:18
问题 I want a function that will return an array of week numbers for current month in current year. For example if the current month is August, 2015 it will return [32, 33, 34, 35, 36]. If month is February, 2015 it will return [6, 7, 8, 9]. Is there a way how can I get this? In apple docs NSCalendar class has an option to get the number of current week in year but It's not what I need. 回答1: As already mentioned NSCalendar has a method rangeOfUnit:inUnit:forDate: . The proper calendar units are

Issues with initWithFormat value from server

送分小仙女□ 提交于 2019-12-13 09:04:33
问题 Please how do I make my initWithFormat to display in as 8:00am - 2:30pm? Help please!! I have already added "dd-MM-yyyy 'at' h:mm:ss a" //opening NSString *startDate= ((Timeslot *)[self.timeslots objectAtIndex:i]).startDateTime; NSArray *split =[startDate componentsSeparatedByString: @"T"]; NSString* clock =[split objectAtIndex: 1]; NSArray *aux= [clock componentsSeparatedByString:@":"]; NSString *startTime = [[NSString alloc] initWithFormat:@"%@:%@",[aux objectAtIndex:0],[aux objectAtIndex:1