nscalendar

Weird behavior when creating an NSDate of January the 1st and the 2nd

会有一股神秘感。 提交于 2019-12-02 09:58:47
问题 I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %d\n", year); //minutes, hours, day and month are initialized elsewhere... NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:year]; [comps

iPhone SDK, how to get NSDate object of coming friday's 20:00?

吃可爱长大的小学妹 提交于 2019-12-02 08:46:43
问题 Does anyone know how to get the NSDate of the coming friday's 20:00 ? 回答1: Yes, this article teaches you how to get the current week's Sunday. I quickly adapted it to get friday at 20:00 (Assuming a Gregorian calendar) NSDate *today = [[NSDate alloc] init]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // Get the weekday component of the current date NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today]

Weird behavior when creating an NSDate of January the 1st and the 2nd

蓝咒 提交于 2019-12-02 07:50:55
I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %d\n", year); //minutes, hours, day and month are initialized elsewhere... NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; [comps setHour:hours]; [comps setMinute:minutes]; NSDate *sampleDate =

NSDateComponents weekOfYear returns wrong value

别来无恙 提交于 2019-12-02 03:38:01
问题 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601]; [calendar setTimeZone:[NSTimeZone defaultTimeZone]]; [calendar setFirstWeekday:2]; [calendar setMinimumDaysInFirstWeek:1]; NSDateComponents *mockTodayComponents = [[NSDateComponents alloc] init]; mockTodayComponents.day = 28; mockTodayComponents.month = 12; mockTodayComponents.year = 2015; NSDate *date = [calendar dateFromComponents:mockTodayComponents]; NSDateComponents *c = [calendar

Sending NSCFCalendar nil NSDate Sarcastic Error [closed]

一曲冷凌霜 提交于 2019-12-02 02:04:30
A few threads here discuss the new iOS 6 Xcode error that pops up in the console when sending nil dates to Calendar methods: -[__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. The error made me chuckle a bit, but got me to thinking.... The code I've written that results in this error

iOS Get Date from Day of Year

一个人想着一个人 提交于 2019-12-02 00:55:19
The common question on stackoverflow is how to get the Day of Year from a date but how to you get the date from the Day of Year? I'm using the following code to generate the Day of Year but how to I do the converse? // Calculate Day of the Year NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]]; setDayOfYear = dayOfYear; return setDayOfYear; Assuming you know the year you want and the actual time of the date doesn't matter and you have

Fire UILocalNotification in a specific Date

末鹿安然 提交于 2019-12-01 12:56:42
I want to fire a UILocalNotification in a specific Date. If I use this code: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components]; If now it's 3 pm this works fine, but it doesn't when, for example, it's 5 pm. How can I set the notification fire date to "the next 4 pm" ? NSCalendar *gregorian = [[NSCalendar

Fire UILocalNotification in a specific Date

不打扰是莪最后的温柔 提交于 2019-12-01 11:44:03
问题 I want to fire a UILocalNotification in a specific Date. If I use this code: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components]; If now it's 3 pm this works fine, but it doesn't when, for example, it's 5

iPhone NSDate eg. next Friday

最后都变了- 提交于 2019-12-01 08:50:24
I want to create a function which results the date of next Friday but I have no plan how to do it. Has anyone a good hint to me ? E.g. Get current date using NSDate, then use 'components>fromDate:' from NSCalendar to get the NSDateComponents, then add the time difference to next Friday and create a new NSDate and Bob's is your uncle. Here is my working solution for getting the next 5 Sundays in Gregorian calendar: self.nextBeginDates = [NSMutableArray array]; NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]]; int

format a NSDate to DDMMYYYY?

China☆狼群 提交于 2019-12-01 06:39:07
I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP? Try this NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@